How to get rid of Google Plus in an oAuth PHP system

271 views Asked by At

Google Plus support for the Google PHP Client will be soon deprecated. I need some alternatives for the following lines of code, but I haven't been able to find any replacements.

Firstly, the scopes: Google Plus scopes will be deprecated and will be creating errors soon. I've been able to find the following scopes at https://developers.google.com/identity/protocols/googlescopes#oauth2v2 enter image description here

Maybe the solution is going with OpenID, but I don't know exactly how to integrate the scope in PHP. My current scope settings are the following:

$client->setScopes(array(Google_Service_Plus::PLUS_ME, Google_Service_Plus::USERINFO_EMAIL, Google_Service_Plus::USERINFO_PROFILE));

Secondly, I need to get the profile info back after validating the response token, however, the only function I know other than the OpenID payload function is:

$plus = new Google_Service_Plus($client); // starts google profile (plus) service
$me = $plus->people->get('me'); // saves account info

So, the final question would be: can I get an OpenID id_token response from the Google oAuth system for the Google PHP Client? And if so, how can I declare the scope and get the id_token?


As a reminder: for getting the profile info from and OpenID id_token, you have to execute the following line of code, which will provide most of the profile information that Google Plus used to offer.

$payload = $client->verifyIdToken($id_token);
1

There are 1 answers

0
quiquelhappy On

For setting the scopes:

$client->setScopes("email","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile");

Getting the user info

$oauth2 = new Google_Service_Oauth2($client);
$userInfo = $oauth2->userinfo->get();
$email = $userInfo["email"];
$name = $userInfo["givenName"];
$surname = $userInfo["familyName"];
$pic = $userInfo["picture"];

//$client->revokeToken();

You can also pass an id token in case you want to use that system with

$client->setScopes("openid","email","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile");
$token = $client->setAccessToken($access_token);
$idtoken= $token["id_token"]
$payload = $client->verifyIdToken($id_token);

if ($payload) {
    $email = $payload["email"];
    $name = $payload["given_name"];
    $surname = $payload["family_name"];
    $pic = $payload["picture"];
}

PS: Check deprecated scopes here: Google + API's Deprecated. Some old Google+ scopes call still be used in order to get, for example, email or another type of data.