1

I tried to make an API using apps-script doGet() function. I am able access the content of this API on my browser using appropriate authentication steps but am unable to access the same using another apps-script project.

The API looks as follows and is published as web app.

function doGet(request){
  var parameters = request.parameters;
  var request_id = parameters.id;
  var response_obj = getObjectById(request_id);
  return ContentService.createTextOutput(JSON.stringify(response_obj)).setMimeType(ContentService.MimeType.JSON);
}

and request is simply made using

const content = UrlFetchApp(url).getContentText();

is there a fundamental mistake or is there an authentication procedure that can be carried out so as to access this content

Answer found thanks for the help guys

what i needed was an authentication procedure and apps-script is very generous with that

refer Using AuthToken obtained via ScriptApp.getAuthToken() to call web apps in GAS

I was able to implement the solution using the following snippet for authentication

var token = ScriptApp.getOAuthToken();
  var header = {"Authorization":"Bearer " + token};
  var options = {
    "method":"GET",
    "headers": header,
    "muteHttpExceptions": true
  };
const content =UrlFetchApp.fetch(url, options).getContentText();
yashprkash
  • 11
  • 2
  • If you publish the app with "Anyone, even anonymous" no auth is required. Does it work from browser? From incognito window? – roma Jun 01 '20 at 13:04
  • yes i tried using 'Anyone, even anonymous option' in incognito ... it still asked for a google id but i was able to view the content in incognito after entering it ..... the urlfetch app returns html to the authentication page – yashprkash Jun 01 '20 at 13:08
  • "Run as me", not "User running the app"? – roma Jun 01 '20 at 13:10
  • This is my example of simple web app. https://stackoverflow.com/questions/61924391/google-email-button-link-which-trigger-a-script-for-sheet/61926345#61926345 It isn't exactly your case but it takes parameter and runs function anonymously – roma Jun 01 '20 at 13:11
  • it is still the same ... even with 'Anyone, even anonymous' and 'execute as me' trying the url in incognito it asks for a google id(any) but displays the content – yashprkash Jun 01 '20 at 13:19

1 Answers1

0

The line

const content = UrlFetchApp(url).getTextContent();

should be

const content = UrlFetchApp.fetch(url).getContentText();

See the documentation for UrlFetchApp here.

Aaron Dunigan AtLee
  • 1,860
  • 7
  • 18