2
votes

Google announced that all google plus api were deprecated on 7th march and I'm using google plus api with oauth2 so I'm worried about is my https://www.googleapis.com/plus/v1/people/me is also deprecated if yes than what is the alternative solution of that..

//index.php
<?php 
include('constant.php');
include('google-login-api.php');
// include constant.php
// include google-api library


if(isset($_GET['code'])) {

    $gapi = new GoogleLoginApi();

    // Get the access token 
    $data = $gapi->GetAccessToken(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_REDIRECT_URL, GOOGLE_CLIENT_SECRET, $_GET['code']);





    if(is_array($data) && count($data)>0)
    {
        // Get user information
        $user_info = $gapi->GetUserProfileInfo($data['access_token']);

        echo "<pre>";
        print_r($user_info);
    }
}else{
    //html code
    ?>
    <a class="sign-in sign-google" href="<?php echo GOOGLE_API_URL; ?>"><i class="fa fa-google-plus"aria-hidden="true"></i>Sign In with Google</a>
    <?php
}
?>

//google-login-api.php
<?php

class GoogleLoginApi
{
    public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {  
        $url = 'https://accounts.google.com/o/oauth2/token';            

        $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $url);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
        curl_setopt($ch, CURLOPT_POST, 1);      
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);    
        $data = json_decode(curl_exec($ch), true);
        $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      
        if($http_code != 200) 
            throw new Exception('Error : Failed to receieve access token');

        return $data;
    }

    public function GetUserProfileInfo($access_token) { 
        $url = 'https://www.googleapis.com/plus/v1/people/me';          

        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $url);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
        $data = json_decode(curl_exec($ch), true);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);     
        if($http_code != 200) 
            throw new Exception('Error : Failed to get user information');

        return $data;
    }
}

?>

here is my code which i have using and its run perfectly ...but the question is that Is still running after 7th march

2
This looks like an XY problem. You have a problem, which you solved by using the Google Plus API. Now you want a different solution. Knowing the solution you currently use doesn't tell us a great deal. You should explain the problem you are trying to solve.Quentin
@Quentin this approach is deprecated on 7th march ..Is there any another solution to login with gmail which will not be deprecatedAlvi

2 Answers

0
votes

A rough replacement for the Google Plus API (or at least the plus.people portion of the API) is the Google People API. It fundamentally does the same thing (returns profile information for users), although there are some changes in how the request is done and how the response is formatted. The biggest difference is that you need to request exactly what fields you want to receive.

In order to get the information for the user, you'd set $url with something more like

$fields = 'names,emailAddresses';
$url = 'https://people.googleapis.com//v1/people/me?personFields='+$fields;

You can see the reference for people.get for the possible values for the personFields parameter and the OAuth scopes that are valid for access.

0
votes

Change url from,

$url = 'https://www.googleapis.com/plus/v1/people/me';

to,

$url = 'https://www.googleapis.com/oauth2/v3/userinfo';

I had same issue, solved here