google Api 获取用户电子邮件地址时出错

Error with google Api to get user'sEmail Address

我正在使用 Google_Service_Gmail() 来获取用户信息。但遇到问题 "PHP Fatal error: Cannot call constructor on line 84 in '.../service/Gmail.php'"

here is code image

$obj = new GoogleOAuth();
$obj->access_token =$access;
$obj->refresh_token = $refresh;//$data['refresh_token'];
$obj->token_type = $tokentype;
$obj->expires_in = 3600;

$arr = array();
$client = new Google_Client($arr);

$client->setApplicationName('Get Email');
$client->setClientId('[CLIENT-ID]');
$client->setClientSecret('[CLIENT-SECRET]');
$client->setRedirectUri('[REDIRECT-URI]');

$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);

$client->setAccessType('offline');

$authUrl = $client->createAuthUrl();

$token = json_encode($obj);

if (!$client->getAccessToken()) {
    $client->setAccessToken($token);
}

$gmail = new Google_Service_Gmail($client);
return $gmail->getEmailAddress();

lib文件中提供的Gmail.php代码为

here is the code of the file in google api php client library

我建议您使用 Gmail 快速入门 [1] 开始您的代码,这样您就可以成功检索 Gmail 服务,使用此对象您可以访问 "user" 属性,这是一个 "Users_Resource" [2],应用getProfile 方法获取"Gmail_Profile" [3] 从那里你可以得到电子邮件地址:

 $user = $gmail->user;
 $email = $user->getProfile("me")->getEmailAddress( );

[1] https://developers.google.com/gmail/api/quickstart/php

[2] https://developers.google.com/resources/api-libraries/documentation/gmail/v1/php/latest/class-Google_Service_Gmail_Users_Resource.html

[3]https://developers.google.com/resources/api-libraries/documentation/gmail/v1/php/latest/class-Google_Service_Gmail_Profile.html