Unauthorized 401 error while 'execute as me'

Deadly 提交于 2021-02-05 07:01:48

问题


I am struggling with a web app I have deployed. When the site permission is available to 'all with the link', as the app is set to 'execute as me', a spreadsheet is properly removed from my drive and a new file created. If I restrict access to certain email addresses, the code no longer works.

I tried passing a token using

  var auth = ScriptApp.getOAuthToken();
  var header = {
    "authorization": "Bearer " + auth
  };
  var params = { 'method':'post', 'headers':header, 'muteHttpExceptions':true};
  var response = UrlFetchApp.fetch(fileURL, params);
  Logger.log(response);

Unfortunately the execution response is

<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

A Google Doc is created with the html text on it in my drive, so I think access is available but something has changed with the permissions. Can anyone point me in the right direction?

EDIT:

It looks like the script needs me to sign in again even though I am passing an OAuth2 access token. https://developers.google.com/apps-script/reference/script/script-app#getOAuthToken()

The scope to Google sites is in place, so I'm not sure why I can't get the response to work properly. It's falling down at UrlFetchApp but just passing the OAuth2 token doesn't seem to be enough.


回答1:


I think that you can access the Web Apps with "anyone" using Manifests. Please try to do the following flow.

  • On script editor of the project with the URL fetch method.
    1. View -> Show manifest file
    2. Please add the following oauthScopes to appsscript.json.
      • "oauthScopes": ["https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/drive"]

By this, the access token retrieved by ScriptApp.getOAuthToken() has the above scopes. After this setting, please try to run your script again. In your sceipt, you can access the Web Apps with "anyone" using auth.

If this didn't work, please redeploy Web Apps again. Because the latest version of scripts for Web Apps might not be reflected.

Reference :

  • Manifests

If this was not useful for you, I'm sorry.



来源:https://stackoverflow.com/questions/48812174/unauthorized-401-error-while-execute-as-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!