Google People / Google Contacts API失败

时间:2018-11-06 11:38:13

标签: php google-contacts google-people

我正在使用此库:https://github.com/rapidwebltd/php-google-people-api

这是我的代码:

<?php
require_once __DIR__ . '/vendor/autoload.php';

use RapidWeb\GooglePeopleAPI\GooglePeople;
use RapidWeb\GoogleOAuth2Handler\GoogleOAuth2Handler;

$clientId = '***-***.apps.googleusercontent.com';
$clientSecret = '***';
$refreshToken = '';
$scopes = ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/contacts',
'https://www.googleapis.com/auth/contacts.readonly'];

$googleOAuth2Handler = new GoogleOAuth2Handler($clientId, $clientSecret, $scopes, $refreshToken);
$people = new GooglePeople($googleOAuth2Handler);

$contacts = $people->all();
print_r($contacts);

?>

我收到以下错误消息:

[fivesta1@sg21 googlecontacts]$ php create_contact.php 

Fatal error: Uncaught exception 'Exception' with message '{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}
' in /home/fivesta1/public_html/googlecontacts/vendor/rapidwebltd/php-google-people-api/src/GooglePeople.php:63
Stack trace:
#0 /home/fivesta1/public_html/googlecontacts/create_contact.php(16): RapidWeb\GooglePeopleAPI\GooglePeople->all()
#1 {main}
  thrown in /home/fivesta1/public_html/googlecontacts/vendor/rapidwebltd/php-google-people-api/src/GooglePeople.php on line 63

我在这里想念什么?谢谢...

1 个答案:

答案 0 :(得分:1)

我遇到了同样的错误,这是因为您的刷新令牌已过期。您必须再次运行安装程序:

php vendor/rapidwebltd/php-google-oauth-2-handler/src/setup.php

并授予您的 Google 帐户权限。