Google OAuth 2.0 服务帐户 - 日历 API(PHP 客户端) [英] Google OAuth 2.0 Service Account - Calendar API (PHP Client)
问题描述
我有一个 Google Apps 帐户.我正在尝试在此帐户中操作用户日历.
I have a Google Apps account. I am trying to manipulate a users Calendar within this account.
- 我已经创建了项目,添加了日历 API 服务,并通过 API 控制台创建了一个服务帐户 OAuth 2.0 客户端 ID.
- 我已通过日历设置将生成的电子邮件地址添加到日历以共享日历.
- 我已按照建议的步骤管理 API 访问.客户端名称与 Google Apps 帐户所在的域相同,范围为 "https://www.googleapis.com/auth/calendar".
- 通过各种来源,我能够编译一个脚本,让我能够读取指定的日历事件并向其添加事件.
我不能做的是创建一个子日历.我已通读,https://developers.google.com/accounts/docs/OAuth2ServiceAccount,并且正在尝试通过prn"发送.
What I cant do is create a sub calendar. I have read through, https://developers.google.com/accounts/docs/OAuth2ServiceAccount, and am attempting to send through the "prn".
这是脚本失败的地方;刷新 OAuth2 令牌时出错,消息:'{ "error" : "access_denied" }'.如果我删除了 prn,那么一切都是好"的.日历只是在开发人员电子邮件下创建的.代码:
This is where the script fails with; Error refreshing the OAuth2 token, message: '{ "error" : "access_denied" }'. If I remove the prn, then all is "good". The calendars just get created under the developer email. Code:
<?
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);
?>
任何帮助将不胜感激.
推荐答案
prn 字段仅适用于托管域,前提是域管理员允许您的应用程序(基于客户端 ID)模拟用户.
The prn field works only with hosted domains, if the domain administrator enabled your application (based on client id) to impersonate users.
这篇关于Google OAuth 2.0 服务帐户 - 日历 API(PHP 客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!