弃用的谷歌加 API 的替代解决方案是什么



谷歌宣布所有谷歌加 API 都在 3 月 7 日被弃用,我正在使用带有 oauth2 的谷歌加 api,所以我担心我的 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;
    }
}
?>

这是我使用的代码,它运行得很好......但问题是 3 月 7 日之后仍在运行

Google

Plus API(或至少是API的plus.people部分)的粗略替代品是Google People API。它基本上做同样的事情(返回用户的配置文件信息),尽管请求的完成方式和响应的格式发生了一些变化。最大的区别是您需要准确请求要接收的字段。

为了获取用户的信息,您可以使用更类似的东西设置$url

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

您可以查看 people.get 的参考,了解对访问有效的 personFields 参数和 OAuth 范围的可能值。

将网址从

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

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

我有同样的问题,在这里解决了

最新更新