API之前没有在项目中使用过或被禁用403错误

API has not been used in project before or it is disabled 403 error

我正在使用应用程序脚本连接到 Google 的内容 API。为了通过 oAuth2 进行身份验证,我在我的 appsscript.json 文件中设置了范围。然后我打电话给 ScriptApp.getOAuthToken() 以获取必要的访问令牌。

但是,我收到 403 响应和如下错误:

Content API for Shopping has not been used in project XXXX before or it is disabled. 

Enable it by visiting https://console.developers.google.com/apis/api/shoppingcontent.googleapis.com/overview?project=XXXX then retry. 

If you enabled this API recently, wait a few minutes for the action to propagate to our 
systems and retry

问题是错误中的项目ID与我的服务账号所在的项目ID不匹配。事实上,该项目似乎根本不存在。

我已经为 sheet 的脚本 ID 启用了客户端 ID 并将其添加到 redirect URI 部分,但它似乎去错了地方。我很难理解为什么我会被重定向到这个不存在的项目。

appsscript.json如下:

{
  "timeZone": "Europe/London",
  "dependencies": {
    "libraries": [
      {
        "userSymbol": "OAuth2",
        "version": "41",
        "libraryId": "XXXX"
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
      "https://www.googleapis.com/auth/content",
      "https://www.googleapis.com/auth/script.external_request"
    ]
}

Default Cloud Platform project to your Standard Cloud Platform project

切换脚本
  • Here 是如何更改为标准项目的说明 - 取决于您使用的是新版编辑器还是旧版编辑器。

  • 成功切换到标准项目后 - enable Content API for Shopping 以及您想在 GCP 控制台中使用的任何其他 API。

  • 如果您使用模拟服务帐户,您还需要在管理控制台中为服务帐户分配必要的范围,如here

    所述