Какое альтернативное решение устарела Google Plus API? - PullRequest
0 голосов
/ 30 января 2019

Google объявил, что все google plus api устарели 7 марта, и я использую google plus api с oauth2, поэтому меня беспокоит, что мой https://www.googleapis.com/plus/v1/people/me также устарел, если да, чем альтернативное решениеиз этого ..

//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;
    }
}

?>

вот мой код, который я использую, и он отлично работает ... но вопрос в том, что все еще работает после 7 марта

Ответы [ 2 ]

0 голосов
/ 07 марта 2019

Изменить URL-адрес с,

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

на,

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

У меня возникла та же проблема, решена здесь

0 голосов
/ 30 января 2019

Грубой заменой API Google Plus (или, по крайней мере, части API plus.people) является Google People API .Он в основном делает то же самое (возвращает информацию профиля для пользователей), хотя есть некоторые изменения в том, как выполняется запрос и как форматируется ответ.Самое большое отличие состоит в том, что вам нужно запросить, какие именно поля вы хотите получить.

Чтобы получить информацию для пользователя , вы должны установить для $url нечто более похожее

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

Вы можете увидеть справку для people.get для возможных значений параметра personFields и областей OAuth, которые действительны для доступа.

...