據我所知,以下應該工作。它是從示例代碼中提取的,並且類似請求like this one解析得很好。是的,範圍設置;是的,這個日曆是特別分享的。下面的代碼 - 你會注意到,我只是試圖讓事情在這一點上工作,但錯誤的數據並沒有太大幫助:爲什麼試圖拉取事件列表總是返回404錯誤?
error_reporting(E_ALL);
session_start();
require_once 'google_settings.php';
require_once 'google-api-php-client/autoload.php';
$client_id = GA_CLIENT_ID; //Client ID
$service_account_name = GA_SERVICE_EMAIL; //Email Address
$key_file_location = GA_KEY_FILE_LOCATION; //key.p12
if ($client_id == '<YOUR_CLIENT_ID>' || !strlen($service_account_name) || !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
try {
$client = new Google_Client();
$client->setApplicationName("Just Testing");
$client->setDeveloperKey(GA_API_KEY);
$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();
$optParams = array();
$results = $service->events->listEvents('xyzxyz', $optParams);
echo "<h3>Results Of Call:</h3>\n<pre>";
if (is_array($results)) {
foreach ($results as $item) {
var_dump($item);
echo "\n\n";
}
} else var_dump($results);
echo "\n</pre>";
}
catch (Exception $e) {
echo $e->getMessage();
}
錯誤我總是這個樣子的(無論哪個測試日曆我用 - 是的,我已經單獨實現的共享):
Error calling GET https://www.googleapis.com/calendar/v3/calendars/xyzxyz?key=<key_appears_here>: (404) Not Found
這項工作?我只是沒有看到什麼?
謝謝!
「xyzxyz」是您想要列出事件的日曆標識嗎?您是否已向允許訪問用戶數據的服務帳戶授予域名範圍授權(https://developers.google.com/drive/web/delegation)?此外,您可以在Google API Explorer中嘗試您的請求https://developers.google.com/google-apps/calendar/v3/reference/events/list – SGC 2015-04-01 17:46:25
是的,「xyzxyz」是日曆ID(實際是當然,不同)。是的,我將域名範圍授權給了我的服務帳戶。我的請求在API瀏覽器中工作 - 對不起,我也嘗試過 - 當我在頁面後面查看時,它甚至使用相同的URL與Google進行通信。我完全失望。 – yodarunamok 2015-04-03 02:42:51
再次查看[OAuth文檔](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority)後,我想也許我需要指定一個用戶,我是這樣冒充的:'$ cred - > sub ='[email protected]';',但這沒有幫助。那裏有任何想法? – yodarunamok 2015-04-09 20:41:28