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

问题描述 投票:0回答:1

我正在使用Google_Service_Gmail()来获取用户的信息。但出现问题“ PHP致命错误:无法在'... / service / Gmail.php'的第84行调用构造函数”

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-api google-api-php-client
1个回答
0
投票

[我建议您使用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

© www.soinside.com 2019 - 2024. All rights reserved.