本文介绍了Google access_token未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个移动Web应用程序,该应用程序将访问Google图书API,并允许用户将图书添加到其收藏夹"书架中.这是我第一次使用需要Google授权的API.

I am developing a mobile web application which will access the Google Books API and allow the user to add books to their "favorites" book shelf. Its my first time using an API that requires the Google authorization.

我需要发送授权请求以修改私人用户数据.我(认为)我有适当的access_token,但我不知道该怎么去.

I need to send an authorized request to modify private user data. I (think) I have have the proper access_token but I can't figure out how to get to it.

我正在像这样使用Google登录按钮:

I am using the Google sign in button like so:

` <div class="g-signin2" data-onsuccess="onSignIn"></div> `

我也有这样的身份来向Google标识我的申请:

I also have this to identify my application to Google:

`<meta name="google-signin-client_id" content="12345.apps.googleusercontent.com"> `

我使用自己的Google帐户登录,然后Google按钮切换为已登录".我只是想从那里记录访问令牌:

I sign in with my own Google account and the Google button changes over to "Signed In." I am simply trying to log the access token from there:

` function onSignIn(googleUser) {
  googly = gapi.auth2.getAuthInstance();
  var id_token = googly.currentUser.get().getAuthResponse().access_token;
  console.log(id_token);
} `

我已经尽力尝试可以在文档中找到的所有内容,但是无论我做什么,我都会发现access_token是未定义的.

I've tried everything I can find in the documentation but no matter what I do, I get back that access_token is undefined.

我怀疑我实际上没有访问令牌,但是我不知道如何测试.

My suspicion is that I don't actually have an access token, but I don't know how to test that.

找到access_token的正确方法是什么?拥有后,如何将其发送到API?

What is the correct way to find the access_token? Once I have it, how do I send it to the API?

推荐答案

尝试使用gapi.auth -js#basic-ideas"rel =" nofollow> API客户端库,而不是Identify Platform中的gapi.auth2.您应该找到gapi.auth.getToken().access_token包含所需的访问令牌.

Try using gapi.auth in the API Client Library instead of gapi.auth2 in the Identify Platform. You should find gapi.auth.getToken().access_token contains the access token you need.

也就是说,我相信这是一个错误.如果您alert(JSON.stringify(googleUser))我怀疑您会发现其中埋有一个access_token字段,但是即使文档建议应该有一个.

That said, I believe this is a bug. If you alert(JSON.stringify(googleUser)) I suspect you'll find there is an access_token field buried in there, but getAuthResponse() doesn't contain it, even though the docs suggest there should be one.

如果任何人都可以确认我已正确理解并举报,那就太好了.

If anyone can confirm I'm understanding this correctly and report it, that would be great.

如果您在请求标头中包含response_type作为令牌",它将返回带有currentUser的access_token

If you include the response_type in the request header as 'token' it will return the access_token with the currentUser

这篇关于Google access_token未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:23