php – 使用服务帐户插入Google日历条目

前端之家收集整理的这篇文章主要介绍了php – 使用服务帐户插入Google日历条目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用服务帐户在Google日历上创建条目.我真的很接近这一点,但最后一行不起作用.我让它运行时出现500内部服务错误.否则,程序运行无错误,无论价值多少.

可以在here找到Calendar.PHP文件内容.我尝试调用的insert()方法从该文件的第1455行开始.

<?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的服务帐户的完整示例,我只想发布我的完整解决方案以供参考.除了实现代码之外,还有一些事情需要做,但是:

1)您需要转到Google Developer’s console并将您的帐户标记为“服务帐户”.这将使其与Web应用程序区分开来.重要的区别是,在添加事件之前,系统不会提示用户登录其帐户,因为该帐户属于您的应用程序,而不是最终用户.有关详细信息,请参阅此第article页,从第5页开始.

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

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

4)在Google日历中,您要添加活动,在设置下,点击日历设置,然后点击顶部的“分享此日历”.在“人员”字段中的“与特定人员共享”下,粘贴服务帐户凭据中的电子邮件地址.将权限设置更改为“更改事件”.不要忘记保存更改.

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

评论某些内容是否令人困惑或遗漏.祝好运!

<?PHP

function calendarize ($title,$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,$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 example for service accounts
Google Developers Console for inserting events in API v3
Using OAuth 2.0 to Access Google APIs

猜你在找的PHP相关文章