admin管理员组

文章数量:1410712

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.

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.

I am using the Google sign in button like so:

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

I also have this to identify my application to Google:

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

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

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.

What is the correct way to find the access_token? Once I have it, how do I send it to the 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.

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.

I am using the Google sign in button like so:

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

I also have this to identify my application to Google:

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

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

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.

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

Share Improve this question asked Nov 10, 2015 at 19:18 dc46dc46 152 silver badges6 bronze badges 1
  • This question is a duplicate of the earlier stackoverflow./questions/32877115/… which I just noticed after posting an answer. – not all wrong Commented Apr 13, 2016 at 20:58
Add a ment  | 

2 Answers 2

Reset to default 7

It's so late but if you pass true parameter to getAuthResponse "getAuthResponse(true)" it returns 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.


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.


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

本文标签: javascriptGoogle accesstoken is undefinedStack Overflow