未经授权的客户端Google Calendar API(Google_Service_Exception)

时间:2017-06-26 09:57:46

标签: unauthorized unauthorizedaccessexcepti google-calendar-api

我已设置Google日历API项目。我使用OAuth 2.0通过同意屏幕访问Google Calendar API数据。

我已按照此处提及的流程进行操作:https://developers.google.com/google-apps/calendar/quickstart/php

我正在尝试获取授权Google帐户的所有日历。我收到了以下错误:

<h1>Google_Service_Exception</h1>
{
 "error": "unauthorized_client",
 "error_description": "Unauthorized"
}

以下是获取所有日历列表的代码:

$client = $this->getGoogleCalenderClient($clientSecretPath);

$accessToken = json_decode(file_get_contents($clientSecretPath), true);

$client->setAccessToken($accessToken);

if ($client->isAccessTokenExpired()) {

    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    $result = $s3Client->putObject([
        'Bucket' => $bucketName,
        'Key' => $credentialsPath,
        'Body' => json_encode($client->getAccessToken())
    ]);
}

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();


有谁知道这里发生了什么?我该如何解决这个错误?
感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

  

有谁知道这里发生了什么?我该如何解决这个错误?

如果您使用服务帐户访问该应用程序,

查看此GithubGoogle for domain-wide delegation的官方文档。

以下步骤必须由Google Apps域管理员执行:

  
      
  1. 转到您的Google Apps域管理控制台。
  2.   
  3. 从控件列表中选择安全性。如果您没有看到列出的安全性,请从页面底部的灰色栏中选择更多控件,然后从控件列表中选择安全性。如果您无法看到控件,请确保以域管理员身份登录。
  4.   
  5. 从选项列表中选择高级设置。
  6.   
  7. 在“身份验证”部分中选择管理第三方OAuth客户端访问。
  8.   
  9. 在客户名称字段中输入服务帐户的客户ID。
  10.   
  11. 在“一个或多个API范围”字段中,输入应授予应用程序访问权限的范围列表。例如,如果您的应用需要对Google Drive API和Google Calendar API进行全域访问,请输入:https://www.googleapis.com/auth/drivehttps://www.googleapis.com/auth/calendar
  12.   
  13. 点击授权。
  14.