dongliqin6939 2013-12-02 15:15
浏览 47

使用php和服务帐户创建Google日历活动 - 验证问题

Is it possible to access google calendar service while using service-account-method? 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. 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 (e.g. login).

I learned, that first the application has to authenticate itself against google server, using the service-account-method with a key-file .p12

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 )

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:

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

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

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

  • 写回答

1条回答 默认 最新

  • douwen4178 2014-07-21 10:12
    关注

    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'),

    in where you call the

    $client->setAssertionCredentials();
    

    Google PHP Client API: Insufficient Permission

    Hope this helps

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度