我在尝试使用服务帐户连接到Google的日历API时遇到了一个真正的问题 . 我已按照指南进行操作,但在尝试检索信息时仍然遇到错误 . 我采取的步骤如下:
-
在Google Cloud Platform Console中创建新项目
-
使用域范围委派在凭据下创建服务帐户
-
使用范围https://www.googleapis.com/auth/calendar将客户端ID添加到G Suite-> Security-> Manage API客户端访问
-
在API和服务下启用Calendar API
-
使用以下代码:
require_once('plugin/google-api-php-client-2.2.2/vendor/autoload.php');
putenv('GOOGLE_APPLICATION_CREDENTIALS=inc/service-account-key.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject('email@calendardomain.com');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$service = new Google_Service_Calendar($client);
$calendarId = 'primary';
$calendarListEntry = $service->calendarList->get($calendarId);
echo $calendarListEntry->getSummary();
有了这个,我只是得到错误:
致命错误:未捕获的异常'Google_Service_Exception',消息'{“error”:“unauthorized_client”,“error_description”:“客户端未经授权使用此方法检索访问令牌 . ” }'
当我将电子邮件地址替换为服务帐户中的电子邮件地址时,错误将更改为:
致命错误:未捕获的异常'Google_Service_Exception',消息'{“错误”:{“errors”:[{“domain”:“global”,“reason”:“notFound”,“message”:“Not Found”}] ,“代码”:404,“消息”:“未找到”}}'
任何帮助,将不胜感激 .