谷歌应用脚本 - 在不更改代码后突然"Authorization is required to perform that action"



我在这里看到了答案:我如何停止错误,授权需要在Google Script中执行该操作。这并不能解决我的问题,因为我没有更改任何代码,而且我在第一次运行代码时已经授权了代码。突然我得到了Authorization is required to perform that action误差。

我也尝试了这个链接https://developers.google.com/apps-script/troubleshooting#common_errors,并在编辑器function test() { var a=1}中运行了一个空白函数。它运行正常,没有提示我授权脚本,也没有错误。因此,在遵循谷歌的指示:To authorize the script, open the Script Editor and run any function.我仍然得到Authorization is required to perform that action错误。

我没有做什么花哨的事。该脚本只有一个.gs文件,该文件只有一个函数。我没有使用任何高级服务,也没有将它链接到任何库。

更新:我试图复制文件,然后再次进入脚本编辑器运行代码。这次出现了授权窗口,所以我单击continue。如下所示:代码想要

View and manage the files in your Google Drive. Send email as you Connect to an external service

我点击"接受"。代码开始运行,2秒后Authorization is required to perform that action.现在每次我尝试运行代码,它甚至不给我授权弹出。它只是给我一个错误。

是代码的第6部分。剩下的代码只需再多几行。

  var oauthConfig = UrlFetchApp.addOAuthService("google");
  var scope = "https://docs.google.com/feeds/";
  //make OAuth connection
  oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oauthConfig.setConsumerKey("anonymous");
  oauthConfig.setConsumerSecret("anonymous");
  //get request
  var request = {
    "method": "GET",
    "oAuthServiceName": "google",
    "oAuthUseToken": "always",
    "muteHttpExceptions": true
  };

UrlFetchApp中的oauthConfig已弃用。它于6月26日被关闭。下面的链接将向您展示如何将代码从oauthConfig迁移到Oauth1库。https://developers.google.com/apps-script/migration/oauth-config

编辑:从评论中可以看出,您想要将电子表格保存为驱动器中的pdf格式。下面是一段可以这样做的代码:

  var ss = SpreadsheetApp.openById(fileId);
  var blob = ss.getBlob().setName("new_file_name.pdf");
  DriveApp.createFile(blob);

相关内容

最新更新