【问题标题】:How can I insert data (event) into Google Calendar with PHP?如何使用 PHP 将数据(事件)插入 Google 日历?
【发布时间】:2015-09-05 02:55:54
【问题描述】:

我正在尝试将活动插入 Google 日历,但每次都会出错。我一直在寻找几个小时来获得答案,但我没有找到任何问题。我测试了很多例子,没有成功。我是 Google 日历的新手,对 google api 文档不太了解。

我有一个使用 Google 日历 (Gcal) 作为后端的 Fullcalendar。我可以毫无问题地从 Gcal 读取数据。当我尝试使用 Fullcalendar 显示时,但是当我想将一些测试数据写入 Gcal 时,它给了我错误。我想制作一个用作预订应用程序的日历应用程序。所以我有一个日历,它显示给每个进入页面的人,他们可以预订约会,这些约会存储在我的谷歌日历中。

主要问题是,我真的不知道如何连接到 Google 日历以写入数据。我看到的每个示例,解决问题的方式都不一样,其中专业已经过时,其中一些使用 Sessions,但我不需要 Session,因为我要显示的日历是 FIX,每个人看到的都一样。

代码:

require_once 'Google/src/Google/autoload.php';
require_once "Google/src/Google/Client.php";
require_once "Google/src/Google/Service/Calendar.php";

$client_id = 'XXXXXXXXXX-f9ltk86b2klat20k1osmfbgpu4u1vqse.apps.googleusercontent.com';
$client_secret = 'XXXXXXXXXXXXXXXXXXXXXXX';
$key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Server Key
$email_address = 'XXXXXXXXXXXX-f9ltk86b2klat20k1osmfbgpu4u1vqse@developer.gserviceaccount.com';


$client = new Google_Client();
$client->setApplicationName("Calendar");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setDeveloperKey($key);

$client->setScopes(array(
 'https://www.googleapis.com/auth/plus.login', 
));

$cal = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event();
$event->setSummary('Title');
$event->setDescription('Title');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2013-08-17T16:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2013-08-17T17:00:00.000-07:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('<Calendar ID>', $event);

它产生的异常:

[19-Jun-2015 09:08:59 Europe/Berlin] PHP 致命错误:未捕获的异常“Google_Service_Exception”和消息“调用 POST 时出错” https://www.googleapis.com/calendar/v3/calendars/hpba8d7p1f6l65ruhbl9qigvks%40group.calendar.google.com/events?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX: (401) 需要登录 /var/www/XXXXXXXXXXXXXXXXXXXX/calendar/Google/src/Google/Http/REST.php:110

【问题讨论】:

    标签: php google-calendar-api google-oauth google-api-php-client service-accounts


    【解决方案1】:

    Login Required表示你没有正确认证。

    通过查看您使用的范围,我认为这可能会提示您为什么它不工作

    'https://www.googleapis.com/auth/plus.login'

    您正在传递 Google+ 的范围,如果您想访问 Google 日历数据,则应该传递其中一个 Google 日历 scopes

    使用服务帐号进行身份验证

    <?php
     require_once 'Google/autoload.php';
       session_start();     
    /************************************************   
     The following 3 values an befound in the setting   
     for the application you created on Google      
     Developers console.         Developers console.
     The Key file should be placed in a location     
     that is not accessable from the web. outside of 
     web root.   
    
     In order to access your GA account you must    
     Add the Email address as a user at the     
     ACCOUNT Level in the GA admin.         
     ************************************************/
    $client_id = '[your client]';
        $Email_address = '[your service account email]';     
        $key_file_location = '[Your key]';      
    
        $client = new Google_Client();      
        $client->setApplicationName("Client_Library_Examples");
        $key = file_get_contents($key_file_location);    
    
    // separate additional scopes with a comma   
    $scopes ="https://www.googleapis.com/auth/calendar";    
    $cred = new Google_Auth_AssertionCredentials(    
        $Email_address,      
        array($scopes),     
        $key         
        );      
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {        
        $client->getAuth()->refreshTokenWithAssertion($cred);       
    }       
    $service = new Google_Service_Calendar($client);    
    
    ?>
    

    从教程 PHP Google Calendar service account 中提取的代码

    提示:确保您已授予服务帐户访问相关日历的权限。您只需像其他任何用户一样将服务帐户电子邮件添加到日历。

    【讨论】:

    • 感谢您的快速回答,我使用您提供的信息编辑了我的代码,但它给了我以下例外:[19-Jun-2015 10:10:39 Europe/Berlin] PHP致命错误:未捕获的异常“Google_Auth_Exception”带有消息“无法解析 p12 文件。这是一个 .p12 文件吗?密码是否正确? OpenSSL 错误:错误:0D06B08E:asn1 编码例程:ASN1_D2I_READ_BIO:not enough data' in /var/www/XXXXXXXXXXXXXXX/calendar/Google/src/Google/Signer/P12.php:55 问题是,我复制了我的服务器 Api键入 $key 变量,我没有使用任何密钥文件,因为我找不到它。
    • 您必须使用您获得访问权限的密钥文件,仅删除它对您没有帮助。如果你的车没油了,你能在没有更多油的情况下开车吗?转到 Google 开发者控制台并创建一个密钥。
    • 我创建了 2 个密钥,1 个用于服务器应用程序,1 个用于浏览器应用程序。正如我之前提到的,我将 Server Api Key 复制到 Key 变量中: $key="";我在 [link]console.developers.google.com[link] 上没有看到任何下载密钥选项,他们可能已将其删除,或者我只是找不到它。我一直在搜索谷歌,如何下载密钥文件,但我认为它已被禁用,他们只提供 Api 密钥。
    • 我必须创建一个新的 ClientID - 服务帐户,这就是我获取密钥文件的方式。
    • 现在终于可以正常工作了,我忘了将我的服务帐户电子邮件添加到我的日历中。谢谢你的帮助,你太棒了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多