![](/img/trans.png)
[英]php - google calendar API event insert returns 403 Insufficient Permission
[英]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帳戶中添加電子郵件ID“ test@test-156006.iam.gserviceaccount.com”。
這可能是由於該服務帳戶沒有足夠的日歷API權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.