我对PHP不是很好,我正在尝试创建一个网页,我将导航到该页面,它将显示我私人谷歌日历中的事件列表。这曾经在几年前工作,但谷歌已经改变了API,所以我的旧解决方案被破坏了。我看到了这个帖子:
How do I connect to the Google Calendar API without the oAuth authentication?
我试过了,但看起来这种方法也不再适用了。 (当我说"没有工作"我的意思是我目前在google-api-php-client-2.0.3
安装了最新的public_html/includes/google-api-php-client-2.0.3/
。我发现上面提到的client.php
链接,但其他文件不在新API中。
基本上,我想弄清楚当您输入日历ID时,实现您在此页面上可以执行的操作的PHP会是什么样子: https://developers.google.com/google-apps/calendar/v3/reference/events/list#request
所以我认为这是一个两步过程:验证然后打印输出。我遇到的麻烦是验证。
我试过这个:
<?php
session_start();
require_once "/includes/google-api-php-client-2.0.3/src/Google/client.php";
require_once "/includes/google-api-php-client-2.0.3/src/contrib/Google_CalendarService.php";
const CLIENT_ID = '...';
const SERVICE_ACCOUNT_NAME = 'my service account name';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '...';
$client = new Google_Client();
$client->setApplicationName("...");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
$key)
);
$client->setClientId(CLIENT_ID);
$service = new Google_CalendarService($client);
//Save token in session
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
//And now you can use the code in their PHP examples, like: $service->events->listEvents(...)
?>
但我得到错误:
Warning: require_once(/includes/google-api-php-client-2.0.3/src/Google_Client.php): failed to open stream: No such file
因为API似乎已经改变了。
我找到了这个链接: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
我创建了一个服务帐户,并下载了一个私钥,但我不知道如何处理它: - /
我试过这个: https://developers.google.com/api-client-library/php/auth/web-app
但是当我导航到index.php时,我收到了这个错误:
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in /home/xxx/public_html/includes/google-api-php-client-2.0.3/src/Google/Client.php:841 Stack trace: #0 /home/xxx/public_html/testing_oauth.php(7): Google_Client->setAuthConfig('client_secrets....') #1 {main} thrown in /home/xxx/public_html/includes/google-api-php-client-2.0.3/src/Google/Client.php on line 841
我尝试将其编码到index.php示例中(第103-107行):
// https://developers.google.com/console
'client_id' => 'My service accounts project name',
'client_secret' => 'url link to my client_secret.json file',
'redirect_uri' => null,
'state' => null,
但我得到同样的错误......