1

Trying to this google client API example

https://developers.google.com/identity/sign-in/web/backend-auth

I get my access token via javascript and post it to my PHP via ajax javascript object is below.

expires_at
:
1490552860336
expires_in
:
2223
first_issued_at
:
1490549260336
id_token
:
"eyJhbGciOiJSUzI1NiIsImtpZCI6ImRkMDM4NzVmMzkzMTdiZThlNTc0MDBlNmMzMDYzMmFmNTU4YzJkZTQifQ.eyJhenAiOiI5Mjg3MzEzODM5NzQtc3RsdmFyc291cnZoNmdqaDdvbjkwZWwxOGhjOHVrNXYuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI5Mjg3MzEzODM5NzQtc3RsdmFyc291cnZoNmdqaDdvbjkwZWwxOGhjOHVrNXYuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTQ1MTMzNjU2ODMwMzExNzMwOTgiLCJlbWFpbCI6IndoeXRlYXZlbWVjaGFuaWNhbEBnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6IkF2SUZQaHNjVmFNY1htTEdtand6M2ciLCJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiaWF0IjoxNDkwNTQ5MjU2LCJleHAiOjE0OTA1NTI4NTYsIm5hbWUiOiJUcmF2aXMgS2xlaW4iLCJwaWN0dXJlIjoiaHR0cHM6Ly9saDQuZ29vZ2xldXNlcmNvbnRlbnQuY29tLy1BdzlCUjF4YnVBNC9BQUFBQUFBQUFBSS9BQUFBQUFBQUFBQS9BTWNBWWk5b2ZNV1NtcllGc2pTc0xUcFpZZHhkT0FEaGZBL3M5Ni1jL3Bob3RvLmpwZyIsImdpdmVuX25hbWUiOiJUcmF2aXMiLCJmYW1pbHlfbmFtZSI6IktsZWluIiwibG9jYWxlIjoiZW4ifQ.PBcvfMs9_MO9mcHDtGGyb3VbO-O374tHH7w4gWr8ZMpRDYyynY6ZQd5BxRxlh6b__upzxz6gMYQ29yY__zDnEp4_NrkSp7RWQAcoF-JuY8xM1GtDy5IwEFKPxDh9h-9bGoxCx5jgX7-bu2F5Xk6opwZUzoyyYk5BT1xXpBfAe35CJHK_NAzSfrwEX8vU-hIP9C_BjipXptg5Eo3fi3DxPWRVbrzsscV0DDASm9xEi5ilLkUxBwRZN9bk6hgXXgn2Ts7Mo4B7Rue1oyB_rkNJ3RSgH7ylnHrVePiVwJ_15aKwiD64FgcmdLOruLEm9pADoiAodMVNZ6HAElr3MuWG1w"
idpId
:
"google"
login_hint
:
"AJDLj6JimYVJadfBfXai38izEPbdRaSSCtY5FyzYIVQ1IPE2kad13fq5-9Qwkik2AlWx3_Qlco531-R7LVEwcxvs8Xhw0TvrSg"
session_state
:
Object
token_type
:
"Bearer"

my PHP is as follows

  require_once 'google-api-php-client-2.1.1/vendor/autoload.php';
            // Get $id_token via HTTPS POST.

            $client = new Google_Client(['client_id' =>'my google client id' ]);
            $payload = $client->verifyIdToken($this->input->post('id_token'));
            if ($payload) {
              $userid = $payload['sub'];
              // If request specified a G Suite domain:
              //$domain = $payload['hd'];
            } else {
              // Invalid ID token
            }

So frustrating I've never had luck with google API examples. I can't seem to find anything on the net to get this to work its been days. Please help me. I'm basically looking for an id or token I can verify and save in my database

I'm getting stuck on this function

$payload = $client->verifyIdToken($this->input->post('id_token'));

Screen shot shows how far I get before an error is thrown.

Screen shot of error

I ended up going this route

  $q = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $this->input->post('id_token');
            $json = file_get_contents($q);
            $userInfoArray = json_decode($json,true);
            $googleEmail = $userInfoArray['email'];
            $google_id= $userInfoArray['kid'];

I'm not sure if the kid is a unique id for the user or not but it keeps returning the same value.

Renish Khunt
  • 5,620
  • 18
  • 55
  • 92
Travis Klein
  • 85
  • 1
  • 7
  • 1
    Is that actually your object? And not something like this ? `{expires_at:1490552860336, expires_in:2223, first_issued_at:1490549260336 .... }` note the brackets and comma – Yolo Mar 26 '17 at 18:22
  • No its just the info in the object – Travis Klein Mar 27 '17 at 00:39
  • http://stackoverflow.com/questions/41797238/cant-validate-google-access-token-wrong-number-of-segments This is a person with a similar issue I think – Travis Klein Mar 27 '17 at 00:40

2 Answers2

0

I know this is a really old thread, but wanted to share this possible solution when using the new Google Identity services:

instead of:

$id_token= $_POST['id_token'];
$CLIENT_ID = 'yourclientid......apps.googleusercontent.com';
...

use:

$id_token= $_POST['credential'];
$CLIENT_ID = 'yourclientid......apps.googleusercontent.com';
$client = new Google_Client(['client_id' => $CLIENT_ID]);
$payload = $client->verifyIdToken($id_token); 
if ($payload) {
  ....
}

Hope this helps!

ViperMav
  • 71
  • 3
-2

It worked thanks, here mine

<?php
require_once 'google-api-php-client-2.2.0_PHP54/vendor/autoload.php';

// Get $id_token via HTTPS POST.
$id_token= $_POST['id_token'];
$CLIENT_ID = 'yourclientid......apps.googleusercontent.com';
$client = new Google_Client(['client_id' => $CLIENT_ID]);
$payload = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $id_token;
        $json = file_get_contents($payload);
        $userInfoArray = json_decode($json,true);
        $googleEmail = $userInfoArray['email'];
        $google_id= $userInfoArray['sub'];

if ($googleEmail)
 {
  echo "validated".$googleEmail;
} else {
   echo "inValidate" ;
  }
?>
  • You are making a `new Google_Client` instance, but you never use the resulting `$client`, which also makes your `$CLIENT_ID` unused. Basically, you're just doing a request without using the google client. – robbe clerckx Dec 18 '18 at 17:16