【问题标题】:GoogleUser.getAuthResponse() doesn't contain access_tokenGoogleUser.getAuthResponse() 不包含 access_token
【发布时间】:2016-01-15 03:40:11
【问题描述】:

UPDATE2:我重新审视了这个问题,并仔细按照下面链接的文档解决了这个问题。但首先,对于那些为此苦苦挣扎的人,你是一个很好的伙伴。谷歌的 doco 版本太多了,令人困惑!您的 html 中是否包含 platform.js 或 client.js?你加载 gapi.auth 还是 gapi.auth2?你是使用 gapi.auth2.render 还是 gapi.auth.authorize 还是 gapi.auth2.init 等等。

返回 access_token 的方式(截至本文日期)链接如下。通过使用 platform.js 仔细遵循指南和参考,我设法完成了这项工作。然后使用 gapi.load('drive', callback) 动态加载其他库,例如 client.js。

https://developers.google.com/identity/sign-in/web/listeners https://developers.google.com/identity/sign-in/web/reference

==== 繁荣的原始问题 ====

更新 1: 我更新了代码示例以对 googleUser 对象进行递归搜索。至少这不应该在后续库中中断。

下面是用于处理 Google gapi.auth2.AuthResponse 对象中的 access_token 不在顶层的问题的代码 sn-p...它被隐藏 :( 在对象的深处!

所以它是可检索的,但不是在顶层!!??我注意到这似乎是一个时间问题......一旦应用程序在后续检查中运行了一段时间,它确实包含顶层的访问令牌!

var authResponse = _.googleUser.getAuthResponse();
_.id_token = authResponse.id_token; // Always exists

// access_token should also be a param of authResponse
if (authResponse.access_token) {
  debug("Worked this time?");
  _.access_token = authResponse.access_token;
} else {
  // !!! Internal object access !!!
  debug("Attempt to get access token from base object.");
  _.access_token = _.objRecursiveSearch("access_token", _.googleUser);

  if (_.access_token) {
    debug("Access token wasn't on authResponse but was on the base object, WTF?");
  } else {
    debug("Unable to retrieve access token.");
    return false;
  }
}


_.objRecursiveSearch = function(_for, _in) {
  var r;
  for (var p in _in) {
    if (p === _for) {
      return _in[p];
    }
    if (typeof _in[p] === 'object') {
      if ((r = _.objRecursiveSearch(_for, _in[p])) !== null) {
        return r;
      }
    }
  }
  return null;
}

我猜 getAuthResponse 准备好后会以某种方式提供回调,但我看不到 API 中的哪个位置。 https://developers.google.com/identity/sign-in/web/reference

【问题讨论】:

  • 因为这个原因,我已经有 g+ 登录失败的实例。

标签: google-api google-drive-api google-api-client


【解决方案1】:

我知道这个问题已经相当老了,但是当我在谷歌上搜索“.getAuthResponse() 没有 access_token”时,它首先出现,这就是我来到这里的原因。

所以对于那些在 2016 年(也许更晚)的人来说,这就是我所发现的

.getAuthResponse 上有一个秘密论据,我发现任何地方都没有记录。如果您要在您的应用程序中运行以下内容

console.log(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse);

您会看到以下内容(从我的控制台复制/粘贴)

function (a){if(a)return this.hg;a=.HE;var c=.rf(this.hg);!a.Ph||a.dL| |a.Lg||(delete c.access_token,delete c.scope);return c}

这表明.getAuthResponse() 函数寻找一个参数,据我所知,它甚至不检查它的值——它只是检查它是否存在,然后返回整个对象。如果没有该函数,其余代码将运行,我们可以非常清楚地看到它正在删除两个键:access_tokenscope

现在,如果我们使用和不使用参数调用这个函数,我们可以检查输出的差异。 (注意:我使用 JSON.stringify 是因为尝试从浏览器控制台复制/粘贴对象会导致一些问题)。

console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse()));
console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse(true)));

getAuthResponse() 对象

{ "token_type":"承载者", "login_hint":"<Huge mess of letters>", “expires_in”:2112, "id_token":"<insert your ridiculously long string here>",...}

getAuthResponse(true) 对象

{ "token_type":"承载者", "access_token":"<an actual access token goes here>", "范围":"<whatever scopes you have authorized>", "login_hint":"<another mess of letters>", “expires_in”:2112, "id_token":"<Insert your ridiculously long string here>", ...}

【讨论】:

    【解决方案2】:

    想出了解决这个问题的方法。事实证明,如果我们不在 gapi.auth2.init 中提供登录范围配置,它不会在 getAuthResponse 中返回 access_token。请按照下面给出的方式调用 gapi.auth2.init 并且 access_token 将出现。

    gapi.auth2.init({
      client_id: <googleClientID>,
      'scope': 'https://www.googleapis.com/auth/plus.login'
    })

    【讨论】:

    猜你喜欢
    • 2019-10-15
    • 2020-06-16
    • 2016-10-17
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多