我正在努力解决这个问题:我正在尝试使用服务帐户从PHP脚本在Google日历中创建活动。
这就是我所做的:
这是我的代码:
<?php
require_once 'google_api_src/Google_Client.php';
require_once 'google_api_src/contrib/Google_CalendarService.php';
const CLIENT_ID = 'xxxxxxxxxxxxx.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxxxxxxxxx@developer.gserviceaccount.com';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'google_api_src/xxxxxxxxx-privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Hall Booking");
session_start();
if (isset($_SESSION['token']))
{
$client->setAccessToken($_SESSION['token']);
}
// Load the key in PKCS 12 format
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
$key));
$service = new Google_CalendarService($client);
//Save token in session
if ($client->getAccessToken())
{
$_SESSION['token'] = $client->getAccessToken();
}
?>
我尽可能调试了代码,但在调用SetAssertionCredentials后,令牌始终设置为null。没有PHP错误。
知道出了什么问题,或者如何进一步调试好吗? 我是否必须在api src文件夹中对config.php进行任何更改(我还没有到目前为止)? 应用程序名称是否重要?它用于什么?
答案 0 :(得分:1)
好的 - 事实证明上面的代码工作正常。我的误解在于认为Google_CalendarService执行了身份验证。实际上,只有在对日历进行实际调用时才会执行身份验证(例如插入事件)。
但是,我认为在会话中保存令牌是在错误的地方 - 应该是在执行了一个动作之后。
回答我的其他问题:不,您不必对config.php进行任何更改。应用程序名称可以设置为任何内容(但我不知道它用于什么)。
我遇到的另一个问题是:我使用日历ID“primary”创建了一个事件。这发回了成功消息,但我无法在日历中看到该事件(我只有该帐户的一个日历)。只有当我将'primary'改为'mycalendarid@gmail.com'时才开始工作。对我来说听起来像API中的错误?
答案 1 :(得分:1)
查看演示:http://amazewebs.com/demo
或者获得1:1的帮助:http://amazewebs.com/go-premium
我已经使用了服务帐户,您在代码中使用了错误的网址,或者至少我使用了我的方法没有问题......
请在此处查看我的代码:http://amazewebs.com
或者在这里:
<!-- language: php -->
<?php
ini_set('display_errors', 1);
require_once '../google-api/Google_Client.php';
require_once '../google-api/contrib/Google_CalendarService.php';
session_start();
const CLIENT_ID = '<YOUR-CLIENT-ID-HERE>.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '<YOUR-SERVICE-EMAIL-HERE>@developer.gserviceaccount.com';
const KEY_FILE = '<YOUR-FINGERPRINT-HERE>-privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("<PUT-YOUR-PROJECT-NAME-HERE");
$client->setUseObjects(true); //IF USING SERVICE ACCOUNT (YES)
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* This next snippet of code is commonly written wrong, be sure to use the correct URL specified as below: */
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME, 'https://www.google.com/calendar/feeds/<YOUR-CALENDAR-ID-HERE-WITHOUT-THE-TRAILING"@group.calendar.google.com">/private/full/',
$key)
);
$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('<PUT-AN-EVENT-TITLE-HERE-OR-PASSED-VARIABLE>');
$event->setLocation('<PUT-A-LOCATION-HERE-OR-PASSED-VARIABLE>');
...The Full script can be found @ AmazeWebs.com
&安培;此外,我建议下载较旧的谷歌api库,因为它似乎不适用于他们最新的0.6.7!
...我已经为您下载并打包了正确路径结构中的所有文件:http://amazewebs.com/downloads/google-api.zip
或者,您可以下载每个单独的文件并自行打包,如果您想直接从谷歌下载: http://google-api-php-client.googlecode.com/svn/trunk/src/
祝你好运:)