6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FuelPHPでGoogleカレンダーに予定を追加したい

Last updated at Posted at 2015-04-05

Jenkinsからとか予定を自動で入れたりとか他にも用途で出てきそうですよね。
そんな事をするJenkinsプラグインとかありそうだけど、自分でいろいろ弄ったりしたいので、プログラムで考える。

FuelPHPとかだとoil refine <cmd>とかすぐに作れるからいいなぁ。するとどこからでもコマンドで呼べるし。

さっそくfuelphp gdataとかで検索してみると「mp-php/fuel-packages-gdataでFuelPHPでGoogle APIを扱う」というページがヒット。

FuelPHPは1.7.2を使用してcomposer.json"mp-php/fuel-packages-gdata": "dev-master",の一行を追加

composer.json
    	 :
		<略>
    	 :
    "require": {
        "php": ">=5.3.3",
        "composer/installers": "~1.0",
        "fuel/docs": "1.7.2",
        "fuel/core": "1.7.2",
        "fuel/auth": "1.7.2",
        "fuel/email": "1.7.2",
        "fuel/oil": "1.7.2",
        "fuel/orm": "1.7.2",
        "fuel/parser": "1.7.2",
        "fuelphp/upload": "2.0.1",
        "monolog/monolog": "1.5.*",
        "mp-php/fuel-packages-gdata": "dev-master",
        "michelf/php-markdown": "1.4.0"
    },
    	 :
		<略>
    	 :

その後に、composer.pharを更新

$ php composer.phar self-update
Updating to version f10c71475167a4661225b14560ca0a400d730829.
    Downloading: 100%         
Use composer self-update --rollback to return to version 020e1c214385a986e330f93a29143d7cbd1a677e

そして、先ほど追加したgdataを入れるために更新。しかしなぜかエラー

$ php composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Updating composer/installers dev-master (2d91dc9 => f8c20b4)


                                                                                                                                                              
  [RuntimeException]                                                                                                                                          
  The .git directory is missing from /home/takara/fuelphp-1.7.2/fuel/vendor/composer/installers, see http://getcomposer.org/commit-deps for more information  
                                                                                                                                                              


update [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--lock] [--no-plugins] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--with-dependencies] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [packages1] ... [packagesN]

ググるとvendorを削除するといいという記事を見つけたので削除

$ rm -rf fuel/vendor/*

再度更新、今度はうまく更新できたよう。

$ php composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing composer/installers (dev-master f8c20b4)
    Cloning f8c20b427de1cfe7a28a015c1640ce4e4eef1e33

  - Installing fuel/docs (1.7.2)
    Loading from cache

  - Installing fuel/core (1.7.2)
    Loading from cache

  - Installing fuel/auth (1.7.2)
    Loading from cache

  - Installing fuel/email (1.7.2)
    Loading from cache

  - Installing fuel/oil (1.7.2)
    Loading from cache

  - Installing fuel/orm (1.7.2)
    Loading from cache

  - Installing fuel/parser (1.7.2)
    Loading from cache

  - Installing fuelphp/upload (2.0.1)
    Loading from cache

  - Installing psr/log (dev-master bf2c13d)
    Cloning bf2c13de4300e227d7b2fd08027673a79c519987

  - Installing monolog/monolog (1.5.0)
    Loading from cache

  - Installing google/apiclient (dev-master 306bf45)
    Cloning 306bf45f9961f8e8bb8799d91ea54c0080ecc99f

  - Installing mp-php/fuel-packages-gdata (dev-master e1e22ec)
    Cloning e1e22ecbc7de42f8e49570af0c06c7fe4acb72a7

  - Installing michelf/php-markdown (1.4.0)
    Loading from cache

monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
Writing lock file
Generating autoload files

Google Developers Console へ行ってプロジェクト作成、そこでできた認証用キーファイルをダウンロード。
そして、そのアカウントをカレンダーに編集者として登録。
あとキーファイルとして別にもつのはファイル名やたら長いし、置き場所にも困るので。プログラムの中へセットでbase64 <キーファイル>として取り込む。

fuel/tasks/t01.php
<?php
namespace Fuel\Tasks;
const CLIENT_ID = '.....apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '...@developer.gserviceaccount.com';
class t01 {
    private function _getKeyFile() {
        return(base64_decode(
            "MIIGwAIBAzCCBnoGCSqGSIb3DQEHAaCCBmsEggZnMIIGYzCCAygGCSqGSIb3DQEHAaCCAxkEggMV".
		    	 :
				<>
		    	 :
            "ibIxk09R1qbB/hZdlRbPtIBOAgIEAA==".
            ""
        ));
    }
    public function run() {
        \Package::load('gdata');

        $gdata = \Gdata::forge( 'Calendar');
        $gdata->client->setAssertionCredentials(new \Google_Auth_AssertionCredentials(
            SERVICE_ACCOUNT_NAME,
            array('https://www.googleapis.com/auth/calendar'),
            $this->_getKeyFile()
        ));
        $cal = new \Google_Service_Calendar($gdata->client);
        /**
         * 予定(イベント)の作成
         */
        $calendarId="....@group.calendar.google.com";
        $event = new \Google_Service_Calendar_Event();
        $start_time = new \Google_Service_Calendar_EventDateTime();
        $start_time->setDateTime('2015-04-21T06:00:00+09:00');
        $event->setStart($start_time);
        $end_time = new \Google_Service_Calendar_EventDateTime();
        $end_time->setDateTime('2015-04-21T11:00:00+09:00');
        $event->setEnd($end_time);
        $event->setLocation('予定場所');
        $event->setSummary('予定タイトル');
        $event->setDescription('予定詳細');
        $cal->events->insert($calendarId, $event);
    }
}

これでタスクを実行。

$ oil refine t01

Googleカレンダーを見て予定が入っていれば基本の動作は確認。
あとはCLIENT_IDとかキーファイルの設定をすべてconfigに持って行ってカレンダー名指定ぐらいでアクセスするといいかなぁ

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?