Open
Description
I'm building a login by google for a mobile app and I have this weird issue that took from me 2 days to debug!!
That's the method verifyIdToken($id_token)
always returning false and the token is valid ( that comes from my mobile app ) as I have tested with google apies oAuth2
Dio dio = new Dio();
Response response = await dio.get('https://www.googleapis.com/oauth2/v1/tokeninfo?id_token='+googleKey.idToken);
print(response.data); //contains the token info
By Luck, I removed the client_id and lifted as empty str and walaa it works returning a user response payload
I can't understand why is that!! I'm using the same client_id on a node app in production and it's working fine ( access token not id token ), and I'm moving from node to PHP so I used the same client_id to test.
The Code:
$params = $request->get_params();
$token = isset( $params['token'] ) ? $params['token'] : false;
if ( ! $token ) {
return new \WP_Error( 'no-token', __( 'No token received from Google', 'vivant' ) );
}
if ( ! class_exists( 'Google_Client' ) ) {
include_once plugin_dir_path( HEADLESS_FILE ) . '/vendor/google/apiclient/src/Google/Client.php'; // change path as needed
}
$client = new \Google_Client(); // works without a client_id and not working with a client_id
// $clinet->setClientSer
try {
$payload = $client->verifyIdToken( $token );
return $payload; // for debugging the value
if ( $payload ) {
// whatever
} else {
return new \WP_Error( 'invalid-token', __( 'Token is not valid', 'vivant' ) );
}
} catch ( \Exception $e ) {
return new \WP_Error( $e->getCode(), $e->getMessage() );
}