1

My intent:

I want to call Google Calendar's API acl.list() from a google script using UrlFetchApp.fetch() function.

Problem:

My Google script it self has oauth token when it runs. Problem is that UrlFetchApp.fetch() is a separate http request which requires oauth token to work.

Question:

How do I reuse token used by my (already authorized) apps-script in http request called manually from that script. Is it possible? If not, then how do I generate token for this request?

Rationale:

The ContactsApp is pleasant to work with but it does not have needed ACL functionality yet.

My (not working) code:

function pleaseHelp() {
  var calId = "MY_CORRECT_CALENDAR_ID";
  var url = "https://www.googleapis.com/calendar/v3/calendars/" + calId + "/acl";
  var data = {
    "foo" : "foo data",
    "bar" : "data bar...",
  };
  var payload = JSON.stringify(data);
  var options = {
    "method" : "POST",
    "contentType" : "application/json",
    "payload" : payload
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

gives exception: "401 Login Required" obviously...

Thanks in advance for any help.

Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85

2 Answers2

2

you could try https://gist.github.com/entaq/4079885 and How to authorize with oauth 2.0 from appscript to Google APIs?

Community
  • 1
  • 1
Venkatesh Bachu
  • 2,348
  • 1
  • 18
  • 28
1

you can attach the token to the call you are making.

for example: https://www.googleapis.com/calendar/v3/calendars/{cal id}/acl?access_token='yourtoken'