And*_*ton 7 javascript google-api google-oauth
我们有一个应用程序依赖Google根据我们的谷歌应用程序帐户验证其用户,然后进行一些服务器端验证和组查找.
最近谷歌改变了持有access_token变量的对象的名称,我们需要对其进行身份验证.在文档(https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile)中,它表示access_token可以从getAuthResponse()方法获得,但是当我使用它时,它会以未定义的形式返回.在console.log()之后检查对象会显示除access_token之外的所有其他字段.我担心谷歌将来会再次改变这个对象而没有我们的申请就离开了我们.这是代码.
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<meta name="google-signin-client_id" content="XXX.apps.googleusercontent.com">
<script>
//This happens after the user has authenticated with Google and has been passed
//back to the page
function onSignIn(googleUser) {
//Check to see whether the user is trying to sign out.
if (window.location.href.indexOf("signOut=1") !== -1) {
//Sign them out of the application.
signOut();
//redirect them to the same page, without the signOut query string so they can log back in if want
window.location.href='googlesigninform.html'
return false;
}
//Grab the token, access token and email.
var _id = googleUser.getAuthResponse().id_token; //This works
var _accessToken = googleUser.Ka.access_token; //This works but changed from googleUser.B.access_token
var profile = googleUser.getBasicProfile(); //Works
console.log(googleUser.access_token); //Undefined
console.log(googleUser.getAuthResponse().access_token);//Undefined
//Make a post request to the API
makePostRequest(_id, _accessToken, profile.getEmail());
}
Run Code Online (Sandbox Code Playgroud)
访问access_token变量的正确方法是什么?
好吧,我有一个解决办法,可以从变量中获取 access_token 。
function findAccessToken(googleUser) {
var returnValue;
Object.getOwnPropertyNames(googleUser).forEach(function (val, idx, array) {
console.log(val + ' -> ' + googleUser[val]);
Object.getOwnPropertyNames(googleUser[val]).forEach(function (vals, idxs, arrays) {
if (vals === "access_token") {
console.log("true");
returnValue = googleUser[val][vals];
}
});
});
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
当然,这不是最优雅的解决方案。如果有人能指出正确的方向那就太好了。
| 归档时间: |
|
| 查看次数: |
863 次 |
| 最近记录: |