使用Google Plus API获取用户电子邮件

时间:2018-07-08 02:43:21

标签: php email google-api-php-client

我已经搜索了所有文档,但只得到了姓名,出生日期和个人资料网址,但没有得到我登录的用户电子邮件。那么您有什么解决方案吗? 我已经尝试过

require_once 'vendor/autoload.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setClientId('801374364215- 
lop7t6qd8jis81j2nqmai9pkicd7ikej.apps.googleusercontent.com');
$client->setClientSecret('apowURdC4urZ8U6cvxeolskv');
$client->setRedirectUri('http://localhost/tosrean/node_modules/quickstart.php');
$client->setScopes(array('https://www.googleapis.com/auth/plus.login'));

$plus = new Google_Service_Plus($client);
error_reporting(E_ERROR | E_PARSE);

$code = isset($_GET['code']) ? $_GET['code'] : NULL;

if(isset($code)) {
    try {
        $token = $client->fetchAccessTokenWithAuthCode($code);
        $client->setAccessToken($token);
    }catch (Exception $e){
        echo $e->getMessage();
    }
}

if ($client->getAccessToken()) {
    $profile = $plus->people->get('me');

    echo $profile['emails'][0]['value'];

} else {
    $authUrl = $client->createAuthUrl();
}

0 个答案:

没有答案