Gapi 的身份验证弹出窗口立即关闭,并失败并显示 401 错误

gapi’s auth popup closes immediately and fails with a 401 error

本文关键字:失败 显示 错误 身份验证 窗口 Gapi      更新时间:2023-09-26

我正在为Google日历制作Chrome扩展程序,我需要向Google Cloud Endpoints服务器进行身份验证。为此,我使用 Google APIs Client Library for JavaScript,方法gapi.auth.authorize

var SCOPES = [
  "https://www.googleapis.com/auth/userinfo.email",
  "https://www.googleapis.com/auth/calendar.readonly"
];
function login() {
  gapi.client.oauth2.userinfo.get().execute(function(response) {
    if (response.code) {
      console.log("cannot auth: " + response.code + " (" + response.message + ")");
    } else {
      console.log("authed " + response.email);
    }
  });
}
gapi.auth.authorize({
  client_id: CLIENT_ID,
  scope: SCOPES.join(" "),
  immediate: immediate
}, login);
现在,有时

它有效,有时它不起作用。 在这种情况下,将出现一个弹出窗口并立即关闭,并且response对象包含:

{
  "code": 401,
  "message": "Invalid Credentials",
  "data": [
    {
      "domain": "global",
      "reason": "authError",
      "message": "Invalid Credentials",
      "locationType": "header",
      "location": "Authorization"
    }
  ],
  "error": {
    "code": 401,
    "message": "Invalid Credentials",
    "data": [
      {
        "domain": "global",
        "reason": "authError",
        "message": "Invalid Credentials",
        "locationType": "header",
        "location": "Authorization"
      }
    ]
  }
}
我一生都

无法找出错误的来源,或者为什么它只是偶尔发生。请注意,另一个 Web 应用程序使用完全相同的代码(包括客户端 ID(并且运行良好。

gapi.auth.authorize({
  client_id: CLIENT_ID,
  scope: SCOPES.join(" "),
  immediate: true
}, login);