2015年7月15日 星期三

[PHP] Google Calendar API 初始使用

以前使用 Google API 都是用在 Javascript 端上,
引用個 script 設定個 API KEY使用者就OK了!

但是如果是使用 PHP、Python、JAVA 等後端之類的,狀況與方法就不同了,

這篇是來記錄一下如何使用 Google API for Calendar

首先要準備的東西

1. Google 帳號
2. 設定 Google API console
   2.1 設定 console
   2.2 設定日曆
3. 安裝 PHP 函式庫

以上來源 Google官方 API 說明 連結#[1]

第一個申請 google 帳號就跳過

第二個 設定  Google API console



2.1.1. 前往 API console 網址
如果是第一次開啟 API console 端的話會跳出建立專案的確認視窗



2.1.2. 稍等一段時間就會自動載入專案畫面。
系統預設會建立一個專案的名稱(API Project ),
如要修改可按上方的專案名稱修改。


















2.1.3.這裡我們以 calendar 為主來看
按下左邊的 API和驗證 -> API 找尋 calendar API 點下去後按下 啟用API























2.1.4. 建立一個憑證
點選 API和驗證 -> 憑證 ,右邊按下  建立新的用戶端ID




















選擇 中間的  服務帳號




















會自動下載一個私有金鑰
(*:這個只會讓你下載一次,要收藏好!)

2.1.5. 產生新的 P12 金鑰
這裡我們要產生一個P12 金鑰,按下憑證中央的按鈕。





















*:這個也是只下載一次,也是要收藏好




















2.1.6 紀錄APP使用者ID
切換左邊的權限,可以看到下面圖,先將 服務帳戶 的使用者記錄下來,接下來會用到。
(例:oooooooooooooooo@developer.gserviceaccount.com)






















 目前為止  2.1 設定 console 端差不多結束,接下來設定日曆部分

2.2.1.  進入日曆網址  連結#[2]

這裡預先要準備的是建立一個新的日曆,可在設定中建立
(因為個人如要撈取似乎要用不同的方式,這裡以一個全新的日曆為例)


















2.2.2.進入日曆設定
選取建立好的日曆 (這裡例: API calendar)




















找尋下面有個日曆ID:

oooooooooooooooooooooo@group.calendar.google.com
這個要記一下 !


2.2.3. 設定與特定使用者共用檔案
切換上面的頁籤 [共用此日曆] 在使用者中輸入剛剛紀錄的APP使用者
(oooooooooooooooo@developer.gserviceaccount.com)

權限可以選擇 查看所有活動資訊 或 進行變更或管理共用設定 都可。
新增後記得要按下方的儲存。
這樣子到目前 2.2 步驟也結束了。


再來是設定 PHP 程式端的 libary
Google 提供幾種做法
在這有Google的 quick start 說明網站

https://developers.google.com/google-apps/calendar/quickstart/php  #[3]

也有不少人使用 Zend 套件(google資源很多)


在此用最徒法煉鋼的方式(無特別需安裝專用套件)


3.1 下載libary from GitHub

連結#[4]


3.2 解壓縮目錄

大約會像這樣的名字 google-api-php-client-master
我們最主要的資料夾是 src這裡

3.3 建立PHP檔案
(此檔案修改網路上一個教學)
[目前暫時找不到原始來源@@稍後補上....]
假定位於 google-api-php-client-master 底下

紅色部分是要填上去的資料。


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


$client_id = 'oooooooooooooooooo.apps.googleusercontent.com'; // 用戶端 ID
$Email_address = 'oooooooooooooooooooo@developer.gserviceaccount.com'; // 用戶端 ID 電子郵件地址 
$key_file_location = 'oooooooooo.p12'; // 憑證檔案
$calendar_id = 'oooooooooooooo@group.calendar.google.com'; // Google Calender ID

$client = new Google_Client ();

$client->setApplicationName("XXXX");  // Google Developers Console 中的專案名稱

$key = file_get_contents($key_file_location);
$scope = array('https://www.googleapis.com/auth/calendar');
$cred = new Google_Auth_AssertionCredentials($Email_address, $scope, $key);
$client->setAssertionCredentials($cred);

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

if (!$client->getAccessToken()) {
    echo "Script:get token failed.<br>";
    exit();
}
/* query calendar events */
$service = new Google_Service_Calendar($client);

$params = array(
    'singleEvents' => 'true',
    'orderBy' => 'startTime'  
    );
$calEvents = $service->events->listEvents($calendar_id, $params)->getItems();
echo json_encode($calEvents); 


這樣就會撈出目前日曆的相關資料。筆記到此......

Reference:
1. https://developers.google.com/api-client-library/php/start/get_started
2. https://www.google.com/calendar/
3. https://developers.google.com/google-apps/calendar/quickstart/php
4. https://github.com/google/google-api-php-client

沒有留言:

張貼留言