0

i am using Google_Service_Gmail() to get the users'info. but getting issue "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();

the code of Gmail.php provided in lib files is

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

jdp
  • 3,446
  • 2
  • 30
  • 54

1 Answers1

0

I suggest you start your code with the Gmail quickstart [1] so you can successfully retrieve the Gmail Service, with this object you can access to the "user" property which is a "Users_Resource" [2], apply the getProfile method to obtain the "Gmail_Profile" [3] from where you can get the email address:

 $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

Andres Duarte
  • 3,166
  • 1
  • 7
  • 14