Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google always returns false verifying id token

I have the next code, got directly from google reference (https://developers.google.com/identity/sign-in/web/backend-auth)

public function verifyFromAndroid($idToken=null) {
        if(empty($idToken)) {
            $idToken = self::SAMPLE_ID_TOKEN;
        }
        $client = new Google_Client(['client_id' => self::CLIENT_ID]);
        $payload = $client->verifyIdToken($idToken);
        if ($payload) {
            print_r($payload);
            $userid = $payload['sub'];
            // If request specified a G Suite domain:
            //$domain = $payload['hd'];
        } else {
            var_dump($payload);
            $this->lastError = "Invalid ID token";
            return false;
        }
    }

But this method always returns false, even using a valid id token that is created and working using the oauthplayground online tool.

The next code works fine, using directly the GoogleAccessToken_Verify class. Can someone tell me why the official Google code doesn't work and yes my own code using the official Google-clien-php sdk?

try {
            $verify = new Google_AccessToken_Verify();
            $result = $verify->verifyIdToken($this->idToken);
            if($result) {

                print_r($result);
                $friendlyData = $this->translateData($result, true);
                if(!$friendlyData) {
                    return false;
                }
                return $friendlyData;
            }
            else {
                $this->lastError = "Invalid token verification, no error code";
                return false;
            }
        }
        catch(UnexpectedValueException $ex) {
            $this->lastError = "UnVaEx (Code {$ex->getCode()}): {$ex->getMessage()}";
            return false;
        }
like image 237
mp3man Avatar asked Jul 25 '17 09:07

mp3man


People also ask

What is Id_token in Google?

Google ID Token helpers. Provides support for verifying OpenID Connect ID Tokens, especially ones generated by Google infrastructure. To parse and verify an ID Token issued by Google's OAuth 2.0 authorization server use verify_oauth2_token() .


1 Answers

try adding complete client ID

xxxxxxxxxxxxxx-xxxxx-yy-zz.apps.googleusercontent.com

while initiating the

$client = new Google_Client(['client_id' => self::CLIENT_ID]);

It should work i was also facing the same issue ...

like image 52
Himanshu Singh Avatar answered Sep 21 '22 05:09

Himanshu Singh