不推荐使用的Google plus API的替代解决方案是什么?

时间:2019-01-30 11:11:31

标签: php google-plus google-signin

Google宣布3月7日弃用了所有google plus api,并且我将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;
    }
}

?>

这是我使用的代码,它可以完美运行...但是问题是3月7日之后仍在运行

2 个答案:

答案 0 :(得分:0)

Google People API是Google Plus API(或至少API的plus.people部分)的粗略替代。从根本上讲,它执行相同的操作(为用户返回配置文件信息),尽管在请求的完成方式和响应的格式方面有一些更改。最大的不同是,您需要准确要求要接收的字段。

要获取information for the user,您需要将$url设置为类似的内容

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

您可以在reference for people.get中看到personFields参数的可能值以及对访问有效的OAuth范围。

答案 1 :(得分:0)

更改网址,

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

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

我遇到了同样的问题,已解决here