简体   繁体   English

使用PHP和服务帐户创建Google日历事件-验证问题

[英]Create google calendar events with php and service account - authenticate issue

Is it possible to access google calendar service while using service-account-method? 使用service-account-method可以访问Google日历服务吗? Google says indirectly, it is not possible, because calendar-service is not on the list of supported services in the documentation of service-account-method, but I personally think, it IS possible. Google间接地说,这是不可能的,因为在service-account-method的文档中,calendar-service不在受支持的服务列表中,但我个人认为是可行的。 But how? 但是如何?

I am writing a calendar application in php, and there I want to create calendar events into my google-calendar without user-action (eg login). 我正在用php编写日历应用程序,并且我想在没有用户操作(例如登录)的情况下将日历事件创建到我的google-calendar中。

I learned, that first the application has to authenticate itself against google server, using the service-account-method with a key-file .p12 我了解到,首先,应用程序必须使用带有密钥文件.p12的service-account-method对Google服务器进行身份验证。

I am using the following code for authentication: (downloaded fom here: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php ) 我正在使用以下代码进行身份验证:(在此处下载的网址http : //code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

session_start();
require_once 'google-api/src/Google_Client.php';
require_once 'google-api/src/contrib/Google_CalendarService.php';
require_once 'google-api/src/contrib/Google_PredictionService.php';


$client = new Google_Client();
$client->setApplicationName(GOOGLE_APPLICATION_NAME);
$client->setClientId(GOOGLE_CLIENT_ID);

$keyfile = file_get_contents(GOOGLE_KEY_FILE);

$client->setAssertionCredentials(new Google_AssertionCredentials(
  GOOGLE_SERVICE_ACCOUNT_NAME,
  array('https://www.googleapis.com/auth/prediction'),
  $keyfile)
);

But this login seems not to work. 但是此登录似乎无效。 How do I find out, if server is logged in at that moment? 如果服务器当时已登录,我如何查找?

I want to continue with this: 我要继续:

$cal = new Google_CalendarService($client);
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
  $calList = $cal->calendarList->listCalendarList();  // <- this is line 55
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

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

But in line 55 there comes this error: 但在第55行中出现此错误:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/calendar/v3/users/me/calendarList : (403) Insufficient Permission' in /usr/www/users/leuchtk/html/google-api/src/io/Google_REST.php:66 致命错误:/ usr / www / users / leuchtk /中的消息“调用GET https://www.googleapis.com/calendar/v3/users/me/calendarList:(403 )权限不足”错误消息未捕获的异常“ Google_ServiceException” HTML /谷歌-API / SRC / IO / Google_REST.php:66

Stack trace: #0 /usr/www/users/leuchtk/html/google-api/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /usr/www/users/leuchtk/html/google-api/src/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /usr/www/users/leuchtk/html/google-api/src/contrib/Google_CalendarService.php(205): Google_ServiceResource->__call('list', Array) #3 /usr/www/users/leuchtk/html/i_termine_admin.php(55): Google_CalendarListServiceResource->listCalendarList() #4 /usr/www/users/leuchtk/html/termine_admin.php(113): include('/usr/www/users/...') #5 {main} thrown in /usr/www/users/leuchtk/html/google-api/src/io/Google_REST.php on line 66 堆栈跟踪:#0 /usr/www/users/leuchtk/html/google-api/src/io/Google_REST.php(36):Google_REST :: decodeHttpResponse(Object(Google_HttpRequest))#1 / usr / www / users / leuchtk / html / google-api / src / service / Google_ServiceResource.php(186):Google_REST :: execute(Object(Google_HttpRequest))#2 / usr / www / users / leuchtk / html / google-api / src / contrib / Google_CalendarService.php(205):Google_ServiceResource-> __ call('list',Array)#3 /usr/www/users/leuchtk/html/i_termine_admin.php(55):Google_CalendarListServiceResource-> listCalendarList()#4 / usr / www /users/leuchtk/html/termine_admin.php(113):include('/ usr / www / users / ...')#5 {main}放在/ usr / www / users / leuchtk / html / google-api中/src/io/Google_REST.php,第66行

So it looks like, the application is not logged in. I am completely lost in this code for now. 因此,该应用程序尚未登录。目前,我完全不知道该代码。 Thank you for some little light, what is missing there... 谢谢你的帮助,那里缺少的东西...

Marco 马尔科

You may want to refer to this question. 您可能要参考此问题。 Your issue could be just to add the 您的问题可能只是添加

array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly') , array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly')

in where you call the 在你所说的

$client->setAssertionCredentials();

Google PHP Client API: Insufficient Permission Google PHP客户端API:权限不足

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM