问题-Google API日历-G Suite-服务帐户-PHP

时间:2018-07-19 09:02:06

标签: php google-api google-api-php-client service-accounts google-calendar-api

我在尝试使用服务帐户连接到Google的日历API时遇到了一个实际问题。我已经按照指南进行操作,但是在尝试检索信息时始终会出错。我已采取的步骤如下:

  1. 在Google Cloud Platform Console中创建一个新项目
  2. 在具有域范围委派的凭据下创建服务帐户
  3. 将客户端ID添加到G Suite-> Security-> Manage API客户端访问,范围为https://www.googleapis.com/auth/calendar
  4. 在API的服务下启用Calendar API
  5. 使用了以下代码:

    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();
    

有了这个,我只是得到错误:

  

致命错误:消息为“ {{error”:“ unauthorized_client”,“ error_description”:“未授权客户端使用此方法检索访问令牌”的未捕获异常“ Google_Service_Exception”。 }'

当我将电子邮件地址替换为服务帐户中的电子邮件地址时,错误更改为:

  

致命错误:未捕获的异常'Google_Service_Exception',消息为'{“ error”:{“ errors”:[{“ domain”:“ global”,“ reason”:“ notFound”,“ message”:“未找到” }],“代码”:404,“消息”:“未找到”}}'

任何帮助将不胜感激。

0 个答案:

没有答案