Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. accessToken = GoogleAuthUtil.getToken(
  2. AuthenticatorActivity.this,
  3. Plus.AccountApi.getAccountName(Common.mGoogleApiClient),
  4. "oauth2:https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.login email"
  5. );
  6.  
  7. https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$_POST['google_access_token']
  8.  
  9. if (!isset($_POST['google_access_token'])) {
  10. throw new Exception('missing google_access_token');
  11. }
  12.  
  13.  
  14. $client = new Google_Client();
  15. $client->setApplicationName("GiverHub");
  16.  
  17. $client->setClientId($this->config->item('google_client_id'));
  18. $client->setClientSecret($this->config->item('google_client_secret'));
  19.  
  20. $client->setDeveloperKey($this->config->item('google_developer_key'));
  21. $client->setRedirectUri($this->config->item('google_redirect_uri'));
  22.  
  23. $client->setScopes([
  24. 'https://www.googleapis.com/auth/plus.login',
  25. 'https://www.googleapis.com/auth/plus.me',
  26. 'email',
  27. ]);
  28.  
  29.  
  30.  
  31. try {
  32. $client->authenticate($_POST['google_access_token']); // if i remove this the rest of the code below works! ...
  33. $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$_POST['google_access_token'];
  34. $req = new Google_Http_Request($reqUrl);
  35.  
  36. $io = $client->getIo();
  37. $response = $io->executeRequest($req);
  38.  
  39.  
  40. $response = $response[0];
  41.  
  42. $response = json_decode($response, true);
  43. if ($response === null) {
  44. throw new Exception('Failed to check token. response null');
  45. }
  46.  
  47. if ($response['issued_to'] !== '466530377541-s7cfm34jpf818gbr0547pndpq9songkg.apps.googleusercontent.com') {
  48. throw new Exception('Invalid access token. issued to wrong client id: '. print_r($response, true));
  49. }
  50.  
  51. if (!isset($response['user_id'])) {
  52. throw new Exception('Missing user_id');
  53. }
  54.  
  55. if (!isset($response['email'])) {
  56. throw new Exception('Missing email');
  57. }
  58.  
  59. /** @var EntityUser $user */
  60. $user = Common::create_member_google([
  61. 'id' => $response['user_id'],
  62. 'email' => $response['email'],
  63. 'given_name' => '',
  64. 'family_name' => '',
  65. ]);
  66. $user->login($this->session);
  67. if ($user instanceof EntityUser) {
  68. echo json_encode( [ 'success' => true, 'user' => $user ] );
  69. } else {
  70. echo json_encode( [ 'success' => false, 'msg' => $user ] );
  71. }
  72. } catch(Exception $e) {
  73. echo json_encode(['success' => false, 'msg' => $e->getMessage()]);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement