OAuth2令牌,消息:当我尝试使用OAuth(服务帐户)更新Google日历时返回“{”错误“:”access_denied“}'

时间:2013-03-01 13:21:55

标签: php google-api oauth-2.0 google-calendar-api service-accounts

我正在使用Google Standard Library for PHP来使用日历服务,并且我已通过Google API控制台为OAuth 2.0身份验证设置了服务帐户类型。

我的主要目标是通过批量更新用户的Google日历(例如:user@organisationname.com)(当用户不在线时)。例如。更新用户日历中的活动。

当用户登录应用程序(使用OAuth2.0)时,他/她将为应用程序提供“管理您的日历”,“查看您的日历”以及“在我不使用时执行这些操作”的权限应用“

以下代码用于使用OAuth2.0

登录
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");


$client->setClientId('XXXXX-flue2a9o5ll602ovrhaejlpm9otgjh1r.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXX');
$client->setRedirectUri('http://localhost/testAPI/google-api-php-client/examples/calendar/simple.php');
$client->setDeveloperKey('AIzaSyCGvXRXGMo58ZDswyb4zBkJgRMLcHBRIrI');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);

  $_SESSION['code']=$_GET['code'];

  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

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

if ($client->getAccessToken()) {
  $calList = $cal->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

  echo $_SESSION['code'];


$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>

获得权限后,我必须保存一些内容,以便将来在用户未登录时使用这些权限吗?

以下代码在用户登录时工作正常。但返回刷新OAuth2令牌时出错,消息:'{“error”:“access_denied”}'用户退出时

<?php 

require_once '../src/Google_Client.php';
require_once '../src/contrib/Google_CalendarService.php';

session_start();

const CLIENT_ID = 'XXXXXX.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'XXXX@developer.gserviceaccount.com';

const KEY_FILE = 'f183b8caXXXXXXXXatekey.p12';

$client = new Google_Client();
$client->setApplicationName("XXXXXXXX Calendar Service");

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

$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'),
        $key,
        'notasecret',
        'http://oauth.net/grant_type/jwt/1.0/bearer',
        '363183053@developer.gserviceaccount.com')
);

$client->setClientId(CLIENT_ID);

$cal = new Google_CalendarService($client);

    try{
$cal->events->quickAdd("info@organisationname.com", "SERVICE TEST ");
}catch(Exception $e){

    print_r($e->getMessage());
}

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
    echo $_SESSION['token'] = $client->getAccessToken();
}

如果用户未登录>(如果用户已授予权限),我该怎么做才能更新日历。我应该在用户登录时保存访问代码,以后在我想运行批处理时使用它吗?

BTW什么是关联句柄?

2 个答案:

答案 0 :(得分:3)

实际上,您无需与服务帐户共享日历。需要做的是将域范围的权限委派给您的服务帐户。

您现在创建的服务帐户需要被授予访问您要访问的Google Apps域的用户数据的权限。

以下任务必须由Google Apps域管理员执行: 1.转到Google Apps域的控制面板。该网址应如下所示:https://www.google.com/a/cpanel/mydomain.com

  1. 转到高级工具...&gt;管理第三方OAuth客户端访问权限。

  2. 在客户名称字段中输入服务帐户的客户ID。

  3. 在“一个或多个API范围”字段中,输入应授予您的应用程序访问权限的范围列表。例如,如果您需要在域范围内只读访问Google Calendar API,请输入:https://www.googleapis.com/auth/calendar.readonly

  4. 单击“授权”按钮。

答案 1 :(得分:2)

由于未正确提及您的范围而导致错误。在Google OAuth 2.0范围内定义:

  

表示您的应用正在申请的Google API访问权限。该   此参数中传递的值通知显示的同意页面   用户。数量之间存在反比关系   请求的权限和获得用户同意的可能性。

     

应用程序请求的空间分隔权限集

  • 要解决此问题,您必须先更改范围参数

  • 在其中包含日历范围

  • 然后获取访问令牌,然后尝试更改内容

  • 然后根据API文档

  • 中提供的要求和步骤进行相应更改