0
votes

I tested it in the Google OAuth 2.0 Playground and it looked like I could return info from the site, but when I set up the OAuth2 code from Github, I can't seem to do a UrlFetchApp request as I get
the error returned code 403. Truncated server response: Not authorized to access this feed I am not sure if this is because it is not enabled in the API console, but I can't find it there or under Advanced Google Services.

This is the section of code I am falling down at:

var service = getService();
  if (service.hasAccess()) {
  Logger.log("initial xml has access "service.hasAccess());
    var headers = {
      "Authorization": "Bearer " + service.getAccessToken()
    };
    var MyAttachmentsURL = 'https://sites.google.com/feeds/content/[DOMAIN]/[SITE NAME]?kind=attachment';
    var response = UrlFetchApp.fetch(MyAttachmentsURL, headers);
  };

The script from Github worked for me and I authorized when the message came up. This is what is in my scope tab:

7 OAuth Scopes required by the script:
https://sites.google.com/feeds
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/script.container.ui
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/script.scriptapp
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/userinfo.email

2

2 Answers

0
votes

According to the Protocol Guide's "Authorizing requests with OAuth 2.0" You must activate the Google Sites API in the API Console if you can see that option (Step 2).


The only other issue I can see is the requiring to specify a version as GData-Version: 1.4.

So your code would change to something like this:

var service = getService();
  if (service.hasAccess()) {
  Logger.log("initial xml has access "service.hasAccess());
    var headers = {
      "GData-Version": "1.4",
      "Authorization": "Bearer " + service.getAccessToken()
    };
    var MyAttachmentsURL = 'https://sites.google.com/feeds/content/[DOMAIN]/[SITE NAME]?kind=attachment';
    var response = UrlFetchApp.fetch(MyAttachmentsURL, headers);
  };
0
votes

As long as the scope is mentioned in the code, it doesn't need to be passed, so that wasn't the issue. This was one of many variations I had been trying and I blame missing the post method on it being the wee hours. This code works (for now).

  var service = getService();
  if (service.hasAccess()) {
  Logger.log("initial xml has access "+service.hasAccess());
    var headers = {
//      "GData-Version" : "1.4",
      "Authorization" : "Bearer "+service.token_.access_token
    };
    var params = {"headers": headers, 'method':'get', 'muteHttpExceptions':true};     
    var MyAttachmentsURL = 'https://sites.google.com/feeds/content/[DOMAIN]/[SITE NAME]?kind=attachment';
    var response = UrlFetchApp.fetch(MyAttachmentsURL, params);
    };

It appears that "GData-Version" : "1.4" is returned in the response header so is not needed in the request. What is needed is the access token and although all the other API's seem to be able to make use of .getAccessToken, I had to amend this to .token_.access_token - this may be just for Google Sites.

I appreciate those who had a look at this and thank you Chris for responding.