【问题标题】:Google calendar + codeigniter谷歌日历+codeigniter
【发布时间】:2016-02-16 07:30:48
【问题描述】:

谁能告诉我在哪里可以找到一些关于如何将谷歌日历 API 集成到 codeigniter 框架的信息。
我关注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 对象??

谢谢

重新编辑

ANSWER ABOUT 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(Object(Google_Http_Request), Object(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::execute(Object (Google_Client), Object(Google_Http_Request)) #5 /home/vendor/google/apiclient/src/Google/Service/Resource.php(240): Google_Client->execute(Object(Google_Http_Request)) #6 /home/vendor/ google/apiclient/src/进入/home/vendor/google/apiclient/src/Google/Http /REST.php 在第 110 行

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

谢谢。

【问题讨论】:

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


    【解决方案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";
            }
    

    【讨论】:

    • 谢谢@RajeshPandurangan 我认为我可以解决它,或者至少了解如何解决(我的意思是,我还没有代码,但我认为我可以做到谷歌和挤压大脑的例子),但我会访问网络,我会尝试,但不是现在-我还有其他工作-。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2015-06-20
    • 2016-01-03
    相关资源
    最近更新 更多