【发布时间】: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