如何在没有oAuth身份验证的情况下连接到Google Calendar API?

时间:2012-01-24 22:51:41

标签: oauth google-api google-calendar-api

我一直在研究Google Calendar API和有关身份验证的文档(http://code.google.com/apis/calendar/v3/using.html#auth)。这里提到的用例似乎是编写一个访问用户日历的应用程序。但是,我正在编写一个网页,该网页将访问网站所有者的日历并仅显示该日历的信息。因此,我不希望用户输入他们的Google帐户信息,这是oAuth想要做的事情。

基本上,我正在寻找一种方法来访问单个私有Google日历,并通过将凭据直接传递给服务来对其进行身份验证。

此处有类似的问题:How to use OAuth with Google Calendar to access only ONE calendar? 这似乎表明海报最初是直接传递凭证。此功能是否仍然可用?你如何处理我描述的用例?

3 个答案:

答案 0 :(得分:15)

如果我没错,他们现在已启动服务帐户:https://developers.google.com/accounts/docs/OAuth2ServiceAccount

修改

以下是其预测API的修改

session_start();
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_CalendarService.php";

const CLIENT_ID = '...';
const 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(...)

答案 1 :(得分:11)

我有同样的问题。我发现这个3部分教程正是你想要的。 Web page for bookings
您创建一个直接写入您自己的日历(而不是用户)的网页,这就是用户不必授予您访问日历的权限的原因。 此解决方案适用于我,但您必须直接使用REST进行管理。

我正在寻找一种方法来使用谷歌api php客户端,因为它似乎有点简单。但我还没有找到一种方法来正确编辑google-api-php-client / src / config.php文件,以便您自己验证自己而不是实际用户,因为您需要访问日历。如果有人有想法或脚本可以做到这一点,我将非常感激。

答案 2 :(得分:0)

也可以在这里看看:

https://github.com/googleapis/google-api-php-client#authentication-with-service-accounts

创建服务帐户后,您无需进行用户提示即可向服务拥有的日历中添加项目,就可以进行身份​​验证。