使用服务帐户访问Google日历活动:{“error” :“access_denied” }。没有Google应用程序 [英] Access Google calendar events from with service account: { "error" : "access_denied" }. No google apps

查看:132
本文介绍了使用服务帐户访问Google日历活动:{“error” :“access_denied” }。没有Google应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用服务帐户访问Google日历。这是我的代码:
< NUMBER> 被Google API Console上的正确值代替。

 <?php 

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

const CLIENT_ID ='< NUMBER> .apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME ='< NUMBER> @ developer.gserviceaccount.com';
const MY_EMAIL ='< MY NAME> @ gmail.com';
const KEY_FILE ='privatekey.p12';

$ client = new Google_Client();
$ client-> setClientId(CLIENT_ID);
$ client-> setApplicationName(< APP NAME>);

$ key = file_get_contents(KEY_FILE);
$ client-> setAssertionCredentials(新的Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
数组('https://www.googleapis.com/auth/calendar'),
$ key,
'notasecret',
'http://oauth.net/grant_type/jwt/1.0/bearer',
MY_EMAIL)
);

$ cal = new Google_CalendarService($ client);
$ calList = $ cal-> calendarList-> listCalendarList();

print< h1>日历列表< / h1>< pre> 。 print_r($ calList,true)。 < /预> 中;

当我执行我的代码时,我收到:


致命错误:带有消息
的未捕获异常'Google_AuthException'错误刷新OAuth2令牌,消息:'{error:
access_denied}''in
/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php:279
堆栈跟踪:#0
/ home / www / 65683f67e3f0d94b14bba3c945014cda / web / intranet / googleapi / auth / Google_OAuth2.php(256):
Google_OAuth2-> refreshTokenRequest(Array)#1
/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php(209):
Google_OAuth2-> refreshTokenWithAssertion()#2
/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/service/Google_ServiceResource.php(166):
Google_OAuth2-> sign(Object(Google_HttpRequest)) #3
/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/contrib/Google_CalendarService.php(154):
Google_ServiceResource - > __ call('list',Array)#4
/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/testService.php(32):
Google_CalendarListServiceResource-> listCalendarList()# 5 {main}
抛出
/home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php
on line 279




如果我改变我的代码:

  $ client-> setAssertionCredentials新的Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
数组('https://www.googleapis.com/auth/calendar'),
$ key));

我收到:


(403)Access未配置


这里出了什么问题?

解决方案

我有解决方案。
首先代码是最后一个:

  $ client-> setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
数组('https://www.googleapis.com/auth/calendar'),
$ key));

问题不在于代码,而在于我的Google帐户,我在Google API Console中提到了Referer 。删除该字段后,代码将生效。



有关信息,如果您想访问共享日历,请不要忘记与您的XXXXXX @开发人员共享日历。


> https://groups.google .com / forum /?fromgroups =#!topic / google-ajax-search-api / kaKYuUstwB0 / discussion


I would like to access google calendar using Service account. This is my code : <NUMBER> is replace by the correct value take on Google API Console.

<?php

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

const CLIENT_ID = '<NUMBER>.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME  = '<NUMBER>@developer.gserviceaccount.com';
const MY_EMAIL  = '<MY NAME>@gmail.com';
const KEY_FILE = 'privatekey.p12';

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setApplicationName("<APP NAME>");

$key = file_get_contents(KEY_FILE);
$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)
);

$cal = new Google_CalendarService($client);
$calList = $cal->calendarList->listCalendarList();

print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

When I execute my code, I receive:

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "access_denied" }'' in /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php:279 Stack trace: #0 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php(256): Google_OAuth2->refreshTokenRequest(Array) #1 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php(209): Google_OAuth2->refreshTokenWithAssertion() #2 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/service/Google_ServiceResource.php(166): Google_OAuth2->sign(Object(Google_HttpRequest)) #3 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/contrib/Google_CalendarService.php(154): Google_ServiceResource->__call('list', Array) #4 /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/testService.php(32): Google_CalendarListServiceResource->listCalendarList() #5 {main} thrown in /home/www/65683f67e3f0d94b14bba3c945014cda/web/intranet/googleapi/auth/Google_OAuth2.php on line 279

If I change my code with :

$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key));

I receive:

(403) Access Not Configured

What is going wrong here?

解决方案

I have the solution. First the good code is the last:

$client->setAssertionCredentials(new Google_AssertionCredentials(
      SERVICE_ACCOUNT_NAME,
      array('https://www.googleapis.com/auth/calendar'),
      $key));

The problem is not the code, but my google account, I have mentioned a Referer in Google API Console. After erasing the field, the code works.

For information purposes, if you would like to access shared calendars, do not forget to share your calendar with your XXXXXX@developer.gserviceaccount.com (your SERVICE_ACCOUNT_NAME).

For more information, see:

https://groups.google.com/forum/?fromgroups=#!topic/google-ajax-search-api/kaKYuUstwB0/discussion

这篇关于使用服务帐户访问Google日历活动:{“error” :“access_denied” }。没有Google应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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