Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UrlFetchApp with credentials? Google Scripts

I am trying to use Google Scripts UrlFetchApp to access a website with a basic username and password. As soon as I connect to the site a popup appears that requires authentication. I know the Login and Password, however I do not know how to pass them within the UrlFetchApp.

var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/‎");    Logger.log(response.getContentText("UTF-8")); 

Currently running that code returns "Access Denied". The above code does not contain the actual address I am connecting to for security reasons. A "t" is missing from all the "http" in the code examples because they are being detected as links and Stackoverflow does not allow me to submit more than two links.

How can I pass the Login and Password along with my request? Also is there anyway I can continue my session once I have logged in? Or will my next UrlFetchApp request be sent from another Google server requiring me to login again?

The goal here is to login to the website behind Googles network infrastructure so it can act as a proxy then I need to issue another UrlFetchApp request to the same address that would look something like this:

var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/vuze/rpc?json={"method":"torrent-add","arguments":{"filename":"htp://vodo.net/media/torrents/anything.torrent","download-dir":"C:\\temp"}}‎");    Logger.log(response.getContentText("UTF-8")); 
like image 766
user3586062 Avatar asked May 08 '14 15:05

user3586062


People also ask

Is UrlFetchApp synchronous?

UrlFetchApp methods are synchronous. They return an HttpResponse object, and they do not take a callback.

What is UrlFetchApp?

UrlFetchApp. Fetch resources and communicate with other hosts over the Internet. This service allows scripts to communicate with other applications or access other resources on the web by fetching URLs. A script can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses.


1 Answers

This question has been answered on another else where. Here is the summary:

Bruce Mcpherson

basic authentication looks like this...     var options = {};     options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)}; 

Lenny Cunningham

//Added Basic Authorization//////////////////////////////////////////////////////////////////////////////////////////    var USERNAME = PropertiesService.getScriptProperties().getProperty('username');   var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');    var url = PropertiesService.getScriptProperties().getProperty('url');//////////////////////////Forwarded 

Ports to WebRelay

  var headers = {     "Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)   };    var params = {     "method":"GET",     "headers":headers   };  var reponse = UrlFetchApp.fetch(url, params); 
like image 142
user3586062 Avatar answered Sep 21 '22 18:09

user3586062