【问题标题】:Google OAuth 2.0 Service Account - Calendar API (PHP Client)Google OAuth 2.0 服务帐户 - 日历 API(PHP 客户端)
【发布时间】:2013-08-27 04:55:27
【问题描述】:

我有一个 Google Apps 帐户。我正在尝试在此帐户中操作用户日历。

  • 我已经创建了项目,添加了日历 API 服务并通过 API 控制台创建了服务帐户 OAuth 2.0 客户端 ID。
  • 我已通过日历设置将生成的电子邮件地址添加到日历以共享日历。
  • 我已按照建议的步骤管理 API 访问。客户端名称与 Google Apps 帐户所在的域相同,范围为“https://www.googleapis.com/auth/calendar”。
  • 通过各种来源,我能够编译一个脚本,让我可以读取指定的日历事件并向其中添加事件。

我不能做的是创建一个子日历。我已经阅读了https://developers.google.com/accounts/docs/OAuth2ServiceAccount,并正在尝试通过“prn”发送。

这是脚本失败的地方;刷新 OAuth2 令牌时出错,消息:'{ "error" : "access_denied" }'。如果我删除 prn,那么一切都是“好的”。日历只是在开发人员电子邮件下创建的。代码:

<?
ini_set('display_errors', 1);

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 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
const MY_EMAIL  = 'joe@domain.com';
const KEY_FILE = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12';

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setApplicationName("My App");
$client->setAccessType('offline');
$client->setUseObjects(true);

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

$key = file_get_contents(KEY_FILE);

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
} else {
    $client->setAssertionCredentials(new Google_AssertionCredentials(
        SERVICE_ACCOUNT_NAME,
        array('https://www.googleapis.com/auth/calendar'),
        $key,
        'notasecret',
        'http://oauth.net/grant_type/jwt/1.0/bearer',
        MY_EMAIL)
    );  
}

// even tried setting the email here
$client->setClientId(MY_EMAIL);

$calendar = new Google_Calendar();
$calendar->setSummary('calendarx');
$calendar->setTimeZone('Australia/Brisbane');
$createdCalendar = $cal->calendars->insert($calendar);
?>

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    如果域管理员允许您的应用程序(基于客户端 ID)模拟用户,则 prn 字段仅适用于托管域。

    【讨论】:

    • 我是域管理员,我已按照以下步骤启用应用程序; developers.google.com/accounts/docs/…。这不适合 OAuth 应用程序吗?不是 OAuth 2.0?应用程序和 Google Apps 帐户在同一个域下。
    • 感谢您的回答。在其他一些信息的帮助下,一分钱终于下降了。我使用 Google Apps 帐户所在的域作为客户端 ID。不是我创建 API 项目时生成的客户端 ID。哇!
    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2011-12-25
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多