问题描述
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.
https://developers.google.com/identity/sign-in/web/listenershttps://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 也应该是 authResponse 的参数如果(authResponse.access_token){debug("这次成功了吗?");_.access_token = authResponse.access_token;} 别的 {//!!!内部对象访问!!!debug("尝试从基础对象获取访问令牌.");_.access_token = _.objRecursiveSearch("access_token", _.googleUser);如果(_.access_token){debug("访问令牌不在 authResponse 上,而是在基础对象上,WTF?");} 别的 {debug("无法检索访问令牌.");返回假;}}_.objRecursiveSearch = 函数(_for,_in){无功 r;for (var p in _in) {如果(p === _for){返回_in[p];}如果(typeof _in[p] === '对象'){if ((r = _.objRecursiveSearch(_for, _in[p])) !== null) {返回 r;}}}返回空;}
我猜 getAuthResponse 准备好后会以某种方式提供回调,但我看不到 API 中的位置.https://developers.google.com/identity/sign-in/web/reference
我知道这个问题已经很老了,但是当谷歌搜索.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_token
和 scope
.
现在,如果我们在带参数和不带参数的情况下调用这个函数,我们可以检查输出的差异.(注意:我使用 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":"<一堆乱七八糟的字母>
",expires_in":2112,"id_token":"<在此处插入你的可笑的长字符串>
",...}
getAuthResponse(true) 对象
{token_type":承载",access_token":",范围":
","login_hint":"
<另一堆字母>
",expires_in":2112,"id_token":"<在这里插入你的可笑的长字符串>
",...}
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/listenershttps://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
I know this question is fairly old, but it appears first when googling for ".getAuthResponse() doesn't have access_token," which is how I got here.
So for those of you in 2016 (and maybe later) here's what I have found out
There's a secret argument on .getAuthResponse
, not documented anywhere I have found. If you would run the following in your app
console.log(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse);
You would see that you get the following (copy/pasted from my console)
This shows that the .getAuthResponse()
function looks for an argument, and as far as I can tell doesn't even check its value -- it simply checks if it is there and then returns the whole object. Without that function, the rest of the code runs and we can see very clearly it is deleting two keys: access_token
and scope
.
Now, if we call this function with and without the argument, we can check the difference in the output. (note: I used JSON.stringify because trying to copy/paste the object from my browser console was causing me some issues).
console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse()));
console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse(true)));
getAuthResponse() object
getAuthResponse(true) object
这篇关于GoogleUser.getAuthResponse() 不包含 access_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!