【问题标题】:Authentication with Google Calendar API using Server Account and PHP使用服务器帐户和 PHP 使用 Google Calendar API 进行身份验证
【发布时间】:2014-01-09 15:17:50
【问题描述】:

我正在为此苦苦挣扎:我正在尝试使用服务帐户从 PHP 脚本在 Google 日历中创建一个事件。

这是我所做的:

  • 创建了一个 Google Cloud 项目
  • 启用日历 API
  • 创建了一个 OAuth 2.0 服务帐户,其中包含客户端 ID、电子邮件地址和公钥
  • 已下载密钥文件并将其保存在我的网站中
  • 与在服务帐户中创建的电子邮件地址共享我的日历(具有管理共享权限)

这是我的代码:

<?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 进行任何更改(到目前为止我还没有)? 应用程序名称重要吗?它是干什么用的?

【问题讨论】:

    标签: php api google-calendar-api


    【解决方案1】:

    好的——结果证明上面的代码运行良好。我的误解是认为 Google_CalendarService 执行了身份验证。实际上,只有在实际调用日历(例如插入事件)时才会执行身份验证。

    但是,我认为将令牌保存在会话中的位置错误 - 应该在执行操作之后。

    回答我的其他问题:不,您不必对 config.php 进行任何更改。应用程序名称可以设置为任何内容(但我不知道它的用途)。

    我遇到的另一个问题:我正在使用“主要”日历 ID 创建一个事件。这发回了一条成功消息,但我无法在日历中看到该事件(我只有一个用于该帐户的日历)。只有当我将“primary”更改为“mycalendarid@gmail.com”时,它才开始工作。对我来说,这听起来像是 API 中的错误?

    【讨论】:

      【解决方案2】:

      查看演示:http://amazewebs.com/demo

      或获得一对一的帮助:http://amazewebs.com/go-premium

      我已经使用服务帐户进行了此操作,您在代码中使用了错误的 URL,或者至少我的方法没有问题...

      在这里查看我的代码: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
      

      & 我还建议下载旧的 google api 库,因为它似乎不适用于他们最新的 0.6.7!

      ...我已经为您下载并打包了正确路径结构中的所有文件:http://amazewebs.com/downloads/google-api.zip

      或者,如果您想直接从 google 下载,您可以下载每个单独的文件并自行打包: http://google-api-php-client.googlecode.com/svn/trunk/src/

      祝你好运:)

      【讨论】:

      • @group.calendar.google.com 什么是日历 ID?我在日历 ID 中传递了我的电子邮件 ID,但它不起作用。请帮忙。
      • 我已经通过视频和帮助文档完整地解释了这一点。 amazewebs.com/go-premium
      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 2020-03-20
      • 2015-05-12
      • 1970-01-01
      • 2020-05-21
      • 2013-09-23
      • 2019-08-25
      相关资源
      最近更新 更多