7

http://api.chan15.info/google-stackoverflow.html

This is the sample code I use to let user login via Google JavaScript API, and it's work, next step is use user id to login to local server via PHP, but use the user id by JavaScript is pretty danger, the real procedure I want is:

  1. login user via JavaScript API
  2. get access_token from JavaScript
  3. pass the access token to PHP
  4. use access token to Google OAuth to get user id again by PHP
  5. login the user by user id

but I don't know how to get access token.

6
  • The Google login API should redirect to a page of your choosing, that's where you'll get the token on the serverside, and then get the data with the token etc. Commented Apr 16, 2015 at 22:10
  • JavaScript API seems not doing the redirect work Commented Apr 16, 2015 at 22:16
  • It does if you do a hybrid login -> developers.google.com/+/web/signin/server-side-flow Commented Apr 16, 2015 at 22:19
  • The problem is, my company is using PHP 5.1, Google PHP SDK require PHP 5.2+, that's why I need to do the login stuff as tired as this way Commented Apr 16, 2015 at 22:32
  • Wait a minutes, is that mean I can't use the access token and doing curl to get information by myself, like Facebook graph API? Commented Apr 16, 2015 at 22:36

1 Answer 1

5

After the user is logged in to their Google account using the Javascript Oauth2 API the access token can be found here:

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;

I've used this to pass the token along to a separate curl PHP request as well as CORS. Since Google's JS API is still Beta I had to resort to sending a PHP curl request in the past. If you're planning to store the token for access after the user navigates away I'd also also get the token expiration date and call another function that wipes out the stored token whenever expired or explicitly revoked. But for me it was just easier to pull this right after successful login each time since I only needed to call the PHP function once and in real-time with AJAX as a bandaid.

PS: You might want to change the original category from Java to Javascript

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer would be better if it also contained some words of explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.