0

I have a working piece of PHP code that I need to convert to Google Script. The code fetches JSON data from a server (URL) and uses Digest auth (RFC 2617) method to validate credentials for accessing data. I have not been succesful in getting the Google script to work with the UrlFetchApp.fetch(url, opt) line that returns a 401 error (access denied). I guess it has to do on how I pass the user/password paramters in Google.

For reference, this is what the working PHP script looks like:

<?php

   $url = "https://myurl.com/status.php";           // API URL                  
   $user = "ABCDE";                                        // user credential
   $pass = "mypass";                                       // password credential

   $fields = array(
      "device" => 4,
      "from"   => "2010-08-22",
      "to"       => "2010-08-24"
   ) ;              

   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 
   curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass"); 
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
   $result = curl_exec($ch);                                                    // send the request 
   curl_close($ch);

   print_r (json_decode($result));                                           // process the response 

?>

Here is the Google Script code:

function getStatus() {

var opt = {
 serial: "ABCDE",
 pass : "mypass"
};

var url = "https://myurl.com/status.php";
var response = UrlFetchApp.fetch(url, opt);

}

UPDATE: Here is the script that I am getting the 401 errors for:

// https://user:pass@url.com/api/status.php?
//
function getstatus() {

var opt = {};
 opt.headers = {"serial": "pass " + Utilities.base64Encode("ABCD" + ":" + 
"123-AAA")};

var url = "https://url.com/api/status.php";
var response = UrlFetchApp.fetch(url, opt);
}
Frank
  • 27
  • 7
  • Can you edit the code into the question? – Pekka Apr 29 '17 at 15:54
  • Hi Pekka, I added the GScript code on the original post – Frank Apr 29 '17 at 15:57
  • I would move the entire relevant contents of the external document into the question; SO questions are supposed to be able to contain all the information needed to solve the problem. I'll suggest an edit – Pekka Apr 29 '17 at 15:59
  • ... Although I'm not so sure the PHP code is necessary at all; the GScript problem looks pretty straightforward. Are you sure that's how you pass a http auth user name and password in GScript though? – Pekka Apr 29 '17 at 16:02
  • A Google search for `UrlFetchApp http auth` finds this: http://stackoverflow.com/questions/23546255/how-to-use-urlfetchapp-with-credentials-google-scripts – Pekka Apr 29 '17 at 16:04
  • Hi Pekka. It is probably a very straight forward error as you say, I just added the PHP code as an example of a working piece of code. But to quite honest I have no idea if the Google Script is properly coded at all, it was my best guess from reading similar posts (I am not an experienced coder). – Frank Apr 29 '17 at 16:06
  • I'd try the code section titled "Bruce McPherson" in the question I linked to. That looks like it might go in the right direction. – Pekka Apr 29 '17 at 16:07
  • Something like this might be worth a try `var opt = {}; opt.headers = {"Authorization": "Basic " + Utilities.base64Encode("ABCDE" + ":" + "mypass")};` (Replace your user name and password accordingly, of course) – Pekka Apr 29 '17 at 16:08
  • But then I've never even *heard* about GScript until now, so no guarantees – Pekka Apr 29 '17 at 16:08
  • I am still getting 401 errors when using the var opt = {}; opt.headers = {"Authorization": "Basic " + Utilities.base64Encode("ABCDE" + ":" + "mypass")} What I basically need is the Google Script equivalent code to send a URL to fetch JSON data formatted as: http://user:pass@someurl.com – Frank Apr 29 '17 at 17:00
  • If this is still an issue post the rest of your fetch options. @Pekka has been pointing you in the right direction for basic authentication. – Spencer Easton May 02 '17 at 17:02
  • I have updated the post with the Google script that I constructed based on Pekka´s suggestion. The failure I get is a 401 with the following detail from the server: {"time":"2017-05-03T18:24:56Z","status":"Access denied"} – Frank May 03 '17 at 18:26

0 Answers0