0
votes

I make a script to generate a form with data from my server, and deploy it as a web app. The script is in a google standard project, and is in develop status with specific user specified for testing. Proper scopes are indicated, and the user's credentials are successfully generated. Following the guide for "execute a function", however, I always got error: {"error": {"code": 404, "message": "Requested entity was not found.", "status": "NOT_FOUND"}}. Below are my codes:

in the code.js:

function doGet(e){
  Logger.log('doGet is called');
  var params = JSON.stringify(e);
  Logger.log('params: '+params);
  var mywork = params.parameters['mywork'];
  var formName = params.parameter['formName'];
  var subject = params.parameter['subject'];
  var result = makeQuestionsForm(mywork, formName, subject);
  return ContentService.createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
}

function doPost(e){
  Logger.log('doPost is called');
  var params = JSON.stringify(e);
  Logger.log('params: '+params);
  var mywork = params.parameters['mywork'];
  var formName = params.parameter['formName'];
  var subject = params.parameter['subject'];
  var result = makeQuestionsForm(mywork, formName, subject);
  return ContentService.createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
}

function makeQuestionsForm(mywork, formName, subject) {
  // code for generating the form
}

My code calling the script:

service = build('script', 'v1', credentials=self.creds)
parameters = {'mywork':self.sheet_body,'subject':request.session['subject'],'formName':title}
api_request = {
            "function": "doPost",
            "parameters": parameters, 
            "devMode": True
}
response = service.scripts().run(body=api_request,
                scriptId=SCRIPT_ID).execute()

I did many tries: specifying function as 'doPost', 'doGet', or 'makeQuestionsForm', specifying scriptId as the id of project or id of script. All got the "NOT_FOUND" result.

Alternatively, I tried urlopen method directly calling the script's url, with token provided at request header. Got not-authorized.

Can you help see, what I did wrong or missed? Thanks

2
Have you managed to do it by calling a simple function without passing parameters into it?Kessy
Hi, Kessy, I just tried, got the same error. By the way, I also tried deploy for execute as Me, or user, using the same email as in my project for generating credentials. All got the same error.David Zhang

2 Answers

0
votes

This is a function that I use to access a webapp from a gmail addon:

function deleteBlackList() {
  const url=Utilities.formatString('%s?mode=dable',burl());
  const params={"muteHttpExceptions":true,"method":"get","headers": {"Authorization": "Bearer " +  ScriptApp.getOAuthToken()}};
  const resp=UrlFetchApp.fetch(url,params);
  const ts=Utilities.formatDate(new Date(), "redacted", "E MMM dd,yyyy HH:mm:ss");
  Logger.log(url);
  Logger.log(resp.getContentText("UTF-8"));
  var action=CardService.newAction().setFunctionName('buildEmailFilterUI');  
  return CardService.newCardBuilder()
  .setHeader(CardService.newCardHeader().setTitle('Deleted BlackListed Emails')).addSection(CardService.newCardSection()
  .addWidget(CardService.newTextParagraph().setText(Utilities.formatString('TimeStamp: %s\n%s\n',ts,resp.getContentText()))))
  .addSection(CardService.newCardSection().addWidget(CardService.newTextButton().setText('Return to Opening Form').setOnClickAction(action)))
  .build();
}

The burl is the exec version of the url and I just hardwire it into the addon.

0
votes

I found the problem. To execute a function, the project has to be deployed as an API executable. It was deployed as web apps, that's why it's NOT FOUND. After deploy the project as an api executable, now, I can access the function. Permission denied though at the moment, but that's another issue.