我有一个谷歌应用程序帐户。我正在尝试操作此帐户中的用户日历。
我不能做的是创建子日历。我通读了,,https://developers.google.com/accounts/docs/OAuth2ServiceAccount,我正试图通过“prn”发送。
这就是脚本失败的地方;刷新OAuth2令牌时出错,消息:“{”错误“:“访问被拒绝”}”。如果我删除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);
?>
任何帮助将不胜感激。
prn字段仅适用于托管域,如果域管理员启用您的应用程序(基于客户端ID)来模拟用户。