GoogleUser.getAuthResponse()不包含access_token [英] GoogleUser.getAuthResponse() doesn't contain access_token

查看:287
本文介绍了GoogleUser.getAuthResponse()不包含access_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPDATE2:我重新审视了这个问题,并仔细地按照下面链接的doco解决了问题。但首先,对于那些正在为此苦苦挣扎的人,你是一个好公司。 Google的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。
$ b 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不是在顶层...它被隐藏:(在对象的深处!)

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

  var authResponse = _.googleUser.getAuthResponse(); 
_.id_token = authResponse.id_token; //总是存在

// access_token should (authResponse.access_token){
debug(Worked this time?);
_.access_token = authResponse.access_token;
} else {
// !!!内部对象访问!!!
debug(尝试从基础对象获取访问标记。);
_.access_token = _.objRecursiveSearch(access_token, _。 googleUser);
$ b $ if(_.access_token){
debug(访问令牌不在authResponse上,但在基础对象上,WTF?);
} else {
debug(Unable to retrieve access token。);
返回false;
}
}


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

我猜getAuthResponse在它准备好后会提供一个回调,但是我无法看到API的位置。
https://developers.google.com/identity/sign-in/web/reference

解决方案

找出解决方法。事实证明,如果我们不在 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'}) 


UPDATE2: I revisited this issue and have solved the problem by carefully following the doco linked below. But first, for those who are struggling with this, you are in good company. There are so many versions of the doco from Google it is confusing! Do you include platform.js or client.js in your html? Do you load gapi.auth or gapi.auth2? Do you use gapi.auth2.render or gapi.auth.authorize, or gapi.auth2.init, and so on.

The way that returns an access_token (as of this article date) is linked below. I managed to get this working by carefully following the guide and reference using platform.js. Other libraries are then dynamically loaded such as client.js using gapi.load('drive', callback).

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

==== ORIGINAL ISSUE FOR PROSPERITY ====

UPDATE 1: I've updated the code sample to do a recursive search of the googleUser object. At least this shouldn't break in a subsequent library.

Below is a code snippet to handle an issue where the access_token in the Google gapi.auth2.AuthResponse object is not at the top level... it is hidden :( in the depths of the object!

So it is retrievable, but not at the top level!!?? I've noticed it seems to be a timing issue... once the application is running for a while on subsequent checks, it does contain the access token at the top level!!

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;
}

I'm guessing getAuthResponse somehow provides a callback once it is ready, but I can't see where in the API. https://developers.google.com/identity/sign-in/web/reference

解决方案

Figured out the fix for this. Turns out that if we don't provide the login scope config in gapi.auth2.init it doesn't return access_token in getAuthResponse. Please call gapi.auth2.init as given below and access_token will be present.

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

这篇关于GoogleUser.getAuthResponse()不包含access_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆