谷歌的OAuth 2.0服务帐户 - 日历API(PHP客户端) [英] Google OAuth 2.0 Service Account - Calendar API (PHP Client)

查看:252
本文介绍了谷歌的OAuth 2.0服务帐户 - 日历API(PHP客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个谷歌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访问的步骤。客户端名称是,谷歌Apps帐户是在同一个域,范围是的https:// WWW .googleapis.com /认证/日历

  • 通过各种渠道,我能够编译脚本,让我读提名的日历事件和事件添加到它。

我不能做的就是创建一个子日历。我已经通过, 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令牌,留言:{错误:ACCESS_DENIED}。如果我删除PRN,那么一切都好。日历刚刚拿到开发商电子邮件下创建。 code:

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);
?>

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

在PRN场仅适用于托管域,如果域管理员启用您的应用程序(基于客户端ID)来模拟用户。

The prn field works only with hosted domains, if the domain administrator enabled your application (based on client id) to impersonate users.

这篇关于谷歌的OAuth 2.0服务帐户 - 日历API(PHP客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆