簡體   English   中英

帶有服務帳戶問題的Google Calendar API,權限不足

[英]Google Calendar API with service account issue, Insufficient permission

Google Calendar API顯示權限不足錯誤。

致命錯誤:出現未捕獲的異常“ Google_Service_Exception”,並顯示消息“調用GET https://www.googleapis.com/calendar/v3/users/me/calendarList出錯:(403)權限不足”

<?php

  session_start();
  include_once "templates/base.php";

  require_once realpath(dirname(__FILE__)'/../src/Google/autoload.php');

  $client_id = '110224493213908548123'; //Client ID .apps.googleusercontent.com
  $service_account_name = 'test@test-156006.iam.gserviceaccount.com'; //Email Address
  $key_file_location = 'test.p12'; //key.p12

  $client = new Google_Client();
  $client->setApplicationName("Easycarcare");
  $client->addScope("https://www.googleapis.com/auth/calendar");
  //$client->addScope("https://www.googleapis.com/auth/calendar.readonly");
  $service = new Google_Service_Calendar($client);



  if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
  }
  $key = file_get_contents($key_file_location);
  $cred = new Google_Auth_AssertionCredentials(
      $service_account_name,
      array('https://www.googleapis.com/auth/calendar'),
      $key
  );

  $client->setAssertionCredentials($cred);
  if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
  }
  $_SESSION['service_token'] = $client->getAccessToken();


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

  while(true) {
    foreach ($calendarList->getItems() as $calendarListEntry) {
      echo $calendarListEntry->getSummary();
    }
    $pageToken = $calendarList->getNextPageToken();
    if ($pageToken) {
      $optParams = array('pageToken' => $pageToken);
      $calendarList = $service->calendarList->listCalendarList($optParams);
    } else {
      break;
    }
  }

您需要記住,服務帳戶不是您。 服務帳戶是虛擬用戶。 它有自己的Google日歷帳戶,自己的Google雲端硬盤帳戶,可能還有更多。

你不會看到日歷列表上的任何數據,直到你先創建一個日歷 ,然后將其添加到日歷列表

請記住,日歷列表不是用戶可以訪問的日歷列表,而只是左下角Web視圖中的顯示。 因此,即使您已經與服務帳戶共享了一個個人日歷,它也不會出現在服務帳戶日歷列表中,除非您以編程方式將其添加到該列表中。

答:您沒有看到任何數據,因為該服務帳戶沒有數據。

您是否嘗試過在Google帳戶中添加電子郵件ID“ test@test-156006.iam.gserviceaccount.com”。

這可能是由於該服務帳戶沒有足夠的日歷API權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM