google-apps-scriptdigest-authentication

Digest Authentication with Google Apps Scripts using UrlFetch


I'm trying to access a URL that is protected by Digest Authentication, using UrlFetch of Google Apps Script.

I managed to access a URL using Basic Authentication:

 var options =
{
  "method" : "get",
  "headers" : {
    "Authorization" : "Basic <Base64 of user:password>"  
  },
};

var response = UrlFetchApp.fetch("http://www.example.com/service/using/basic", options);

and I managed to access with OAuth based on the example in the documentation.

The problem now is that I need to have the same functionality with a site that is implementing Digest. I can access it with the following curl:

curl -v --digest --user user:password http://www.example.com/service/using/digest

Updated

If I try to simply call

var options =
{
  "method" : "get",
  "headers" : {
    "Authorization" : "Digest " + Utilities.base64Encode( Utilities.computeDigest(user:password) )
  },
};

It doesn't work due to the Challenge-Response mechanism of Digest Authentication.

Can I have it with UrlFetch using any JavaScript library that is implementing such functionality? or directly by UrlFetch itself?


Solution

  • HTTP Digest authorization is a bit more complicated then what you have so far. Check out the wiki page or look at this sample Python implementation for more information.

    It should be possible to implement digest auth with Apps Script, using the Utilities methods that Henrique mentioned. As for handling the challenge/response portion of the algorithm, you can use the not-yet-documented UrlFetchApp option muteHttpExceptions as shown here.