Google日历+ codeigniter

时间:2015-11-14 23:01:34

标签: codeigniter google-calendar-api google-api-php-client

有人能告诉我在哪里可以找到有关如何在codeigniter框架上集成google calendar api的信息。
我跟着these steps,似乎一切都很好,但现在我不知道如何继续 我按照步骤(json,库和quickstart.php)获得的所有文件都将它们放在框架的库文件夹中......是不是正确?
有没有关于那个的教程?

修改

我正在做一些尝试,但是我遇到了一些错误...例如我试图效仿this page的例子,看起来很简单:

$calendarListEntry = $service->calendarList->get('calendarId');
echo $calendarListEntry->getSummary();

但是...... $ service得到了什么?我正在查看客户端库的示例,并且有一个文件调用simple-query-php,其中使用了一个名为Google_Service_Books的类,其中对象的名称是$ service,我试图从两个来源混淆,结果就是这样:

require_once('Google/autoload.php');    
$client = new Google_Client();
$client->setApplicationName("My_application_name");
$apiKey = "my-client-secret";
$client->setDeveloperKey($apiKey);

$service = new Google_Service_Books($client);
$calendarListEntry = $service->calendarList->get('calendarId');
echo $calendarListEntry->getSummary();

执行时存在疑问和错误。

怀疑,在这一行:$ apiKey =“my-client-secret”;它通过API-KEY询问,但我把client_secret ......它是一样的吗?

错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Google_Service_Books::$calendarList
Filename: controllers/dashboard.php
Line Number: 17

Fatal error: Call to a member function get() on a non-object in /var/www/html/prototipo/application/controllers/dashboard.php on line 17

如何获得正确的$ service对象?

谢谢

RE-修改

关于APIKEY的答案我认为它不一样,我输入了创建OAuth 2.0客户端ID凭证后获得的客户端密码,现在我创建了一个新的凭证:API KEY->服务器密钥。

我认为现在我正在获得正确的类以获取正确的$ service对象,但我发现了另外一个新问题。我正在尝试的是:

    require_once('Google/autoload.php');    
    $client = new Google_Client();
    $client->setApplicationName("CalendarTest");
    $apiKey = "APY-KEY"; 
    $client->setDeveloperKey($apiKey);

    $service = new Google_Service_Calendar($client);
    $calendarListEntry = $service->calendarList->get('primary');
    echo $calendarListEntry->getSummary();

在浏览器上调用此方法的结果如下:

  

致命错误:在/home/vendor/google/apiclient/src/Google/Http/REST.php:110中显示错误消息'Google_Service_Exception',并显示错误“正在调用GET https://www.googleapis.com/calendar/v3/users/me/calendarList/calendarId?key=MY-APY-KEY :( 401)需要登录”堆栈跟踪:#0 /home/vendor/google/apiclient/src/Google/Http/REST.php(62):Google_Http_REST :: decodeHttpResponse(对象(Google_Http_Request),对象(Google_Client))#1 [内部功能]:Google_Http_REST :: doExecute(Object(Google_Client),Object(Google_Http_Request))#2 /home/vendor/google/apiclient/src/Google/Task/Runner.php(174):call_user_func_array(Array,Array)#3 / home / vendor /google/apiclient/src/Google/Http/REST.php(46):Google_Task_Runner-> run()#4 /home/vendor/google/apiclient/src/Google/Client.php(593):Google_Http_REST ::执行(对象(Google_Client),对象(Google_Http_Request))#5 /home/vendor/google/apiclient/src/Google/Service/Resource.php(240):Google_Client->执行(对象(Google_Http_Request))#6 / home / vendor / google / apiclient / src /进入/ home / vendor / google / apiclient / src / Google / Http第110行的/REST.php

如果删除有关applicationName和apikey的行,结果相同。要求登录??这是什么意思??

谢谢。

1 个答案:

答案 0 :(得分:1)

按照页面http://www.daimto.com/google-calendar-api-with-php-service-account/创建了如下所示的代码,该代码正常运行。

在此代码中,我从国家印度的谷歌日历获取公众假期列表

    <?php

require 'vendor/autoload.php';

define('APPLICATION_NAME', 'MY_APPLICATION_NAME'); 
define('CREDENTIALS_PATH', 'credentials/SERVICE_ACCOUNT.p12'); 
define('EMAIL_ADDRESS', 'holidays@mycalndear.iam.gserviceaccount.com');


/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
    //below lines can be left as it is
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);

    $key = file_get_contents(CREDENTIALS_PATH);

    // separate additional scopes with a comma
    $scopes ="https://www.googleapis.com/auth/calendar.readonly";
    $cred = new Google_Auth_AssertionCredentials(
        EMAIL_ADDRESS,
        array($scopes),
        $key
    );

    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    return $client;
}

$client = getClient();
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
$calendar_id = "en-us.indian#holiday@group.v.calendar.google.com";



        // get events
        $events = $service->events->listEvents($calendar_id);


        foreach ($events->getItems() as $event) {
            echo "Summary : ".$event->getSummary()." status : " . $event->getStatus() . " starts at : " . $event->getStart()->date . " ends at : " . $event->getEnd()->date . " html link : " . $event->getHtmlLink() ."\n";
        }