使用服务帐户插入Google日历条目 [英] Inserting Google Calendar Entries with Service Account

查看:124
本文介绍了使用服务帐户插入Google日历条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用服务帐户在Google日历上创建条目.我真的很接近这一点,但是最后一行是行不通的.当我让它运行时,我得到一个500 Internal Service Error.否则,该程序将无错误运行,无论任何有价值的操作.

I'm trying to use a service account to create entries on a Google calendar. I'm really close on this, but the very last line won't work. I get a 500 Internal Service Error when I let this run. Otherwise, the program runs error free, for whatever that is worth.

Calendar.php文件的内容可以在此处.我尝试调用的insert()方法从该文件的第1455行开始.

The Calendar.php file contents can be found here. The insert() method that I am trying to call begins on line 1455 of that file.

<?php

function calendarize ($title, $desc, $ev_date, $cal_id) {

    session_start();

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/
    set_include_path( '../google-api-php-client/src/');

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';

    // (not real keys)
    $client_id = '843319906820-jarm3f5ctbtjj9b7lp5qdcqal54p1he6.apps.googleusercontent.com';
    $service_account_name = '843319906820-jarm3f5ctbtjj7b7lp5qdcqal54p1he6@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client/calendar-249226a7a27a.p12';

//    echo pageHeader("Service Account Access");
    if (!strlen($service_account_name) || !strlen($key_file_location))
        echo missingServiceAccountDetailsWarning();

    $client = new Google_Client();
    $client->setApplicationName("xxxx Add Google Calendar Entries");

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

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name, 
        array('https://www.googleapis.com/auth/calendar'), 
        $key
    );
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();

    // Prior to this, the code has mostly come from Google's example
    //     google-api-php-client / examples / service-account.php
    // and relates to getting the access tokens. 

    // The rest of this is about setting up the calendar entry.
    //Set the Event data
    $event = new Google_Service_Calendar_Event();
    $event->setSummary($title);
    $event->setDescription($desc);

    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDate($ev_date);
    $event->setStart($start);

    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDate($ev_date);     
    $event->setEnd($end);

    $calendarService = new Google_Service_Calendar($client);
    $calendarList = $calendarService->calendarList;

    $events = $calendarService->events;

    // if I leave this line, my code won't crash (but it won't do anything, either)
    //echo "here"; die(); 

    $events.insert($cal_id, $event, false);
} 

?>

推荐答案

我知道了.由于我看不到在API v3中使用服务帐户的完整示例,因此我将发布完整的解决方案以供参考.但是,除了实现代码外,还需要做一些事情:

I figured this out. Since I don't see any complete examples of using service accounts with API v3, I'm just going to post my complete solution for reference. There are a few of things that you need to do in addition to implementing the code, however:

1)您需要转到 Google Developer的控制台,并将您的帐户标记为服务帐户".这会将其与Web应用程序区分开.重要的区别在于,添加事件之前不会提示任何人登录其帐户,因为该帐户属于您的应用程序,而不是最终用户.有关更多信息,请参阅从第5页开始的文章.

1) You need to go to the Google Developer's console and mark your account as a 'service account'. This will differentiate it from a web application. The important difference is that nobody will be prompted to log in to their account before the events are added since the account will belong to your application, not an end user. For more information see this article, starting on page 5.

2)您需要创建一个公钥/私钥对.在开发人员的控制台中,单击凭据".在您的服务帐户下,单击生成新的P12密钥".您需要将其存储在某个地方.该文件位置成为下面代码中的$key_file_location变量字符串.

2) You need to create a public/private key pair. From the developer's console, click on Credentials. Under you service account, click on 'Generate new P12 key'. You'll need to store this somewhere. That file location becomes the $key_file_location variable string in the code below.

3)同样从开发人员的控制台中,您需要启用Calendar API.在项目的左边缘,您将看到APIs.选择它并找到Calendar API.单击它,接受服务条款,并确认它现在显示在Enabled APIs下且状态为On

3) Also from the developer's console, you need to enable the Calendar API. From your project, on the left margin you'll see APIs. Select that and find the Calendar API. Click it, accept the terms of service, and verify that it is now displayed under Enabled APIs with a status of On

4)在要向其添加事件的Google日历中,在设置"下,单击日历设置",然后在顶部的共享此日历"上.在人员"字段中的与特定的人共享"下,粘贴服务帐户凭据中的电子邮件地址.将权限设置更改为对事件进行更改".不要忘记保存更改.

4) In Google Calendar that you want to add events to, under settings, click Calendar Settings then on 'Share this Calendar' at the top. Under 'Share with specific people' in the 'Person' field, paste in the email address from the service account credentials. Change the permission settings to 'Make changes to events'. Don't forget to save the change.

然后,在某处实现此代码.

Then, implement this code somewhere.

注释是否令人困惑或遗漏.祝你好运!

Comment if something is confusing or omitted. Good luck!

<?php

function calendarize ($title, $desc, $ev_date, $cal_id) {

    session_start();

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/
    set_include_path( '../google-api-php-client/src/');

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';

    //obviously, insert your own credentials from the service account in the Google Developer's console
    $client_id = '843319906820-xxxxxxxxxxxxxxxxxxxdcqal54p1he6.apps.googleusercontent.com';
    $service_account_name = '843319906820-xxxxxxxxxxxxxxxxxxxdcqal54p1he6@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client/calendar-xxxxxxxxxxxx.p12';

    if (!strlen($service_account_name) || !strlen($key_file_location))
        echo missingServiceAccountDetailsWarning();

    $client = new Google_Client();
    $client->setApplicationName("Whatever the name of your app is");

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

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name, 
        array('https://www.googleapis.com/auth/calendar'), 
        $key
    );
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();

    $calendarService = new Google_Service_Calendar($client);
    $calendarList = $calendarService->calendarList;

    //Set the Event data
    $event = new Google_Service_Calendar_Event();
    $event->setSummary($title);
    $event->setDescription($desc);

    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDateTime($ev_date);
    $event->setStart($start);

    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime($ev_date);
    $event->setEnd($end);

    $createdEvent = $calendarService->events->insert($cal_id, $event);

    echo $createdEvent->getId();
} 

?>

一些有用的资源:
服务帐户的Github示例
用于在API v3中插入事件的Google Developers Console
使用OAuth 2.0访问Google API

Some helpful resources:
Github example for service accounts
Google Developers Console for inserting events in API v3
Using OAuth 2.0 to Access Google APIs

这篇关于使用服务帐户插入Google日历条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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