据我所知,以下应该工作。它是从示例代码中提取的,并且类似请求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