gapi的auth弹出窗口立即关闭,并因401错误而失败

时间:2014-09-18 13:21:39

标签: javascript google-oauth

我正在为Google日历制作Chrome扩展程序,我需要向Google Cloud Endpoints服务器进行身份验证。我使用Google API 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"
      }
    ]
  }
}

我不能为我的生活找出错误的来源,或者为什么它偶尔会发生。请注意,另一个webapp使用完全相同的代码(包括客户端ID)并且可以完美地工作。

1 个答案:

答案 0 :(得分:-1)

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