2017年8月15日 星期二

用 Gmail API 讀取 Label 清單

平時的工作中,經常都會在伺服器中加入電郵通知,當有任何值得留意的狀況發生時,我會收到電郵說明內容;因為這樣能比較主動地帶出重要的資訊。然而,很多時候像運行記錄之類的電郵通知都沒有保留在伺服器,電郵一經發出,數據便不再存在。如果拿來作為數據分析或繪畫圖表,相信會有一點啟示。由於公司是使用 Gmail 系統,要做到這點,可以用 Gmail API。

我習慣用 Label 來把電郵分門別類;所以要達到以上目的,我得先取得 Label 的清單。以下是我的代碼:
<?php
//----------------------------------------------------------------------------------------
//  Gmail API DEMO
//----------------------------------------------------------------------------------------
//  Platform: macOS + PHP + Apache
//  Written by Pacess HO
//  Copyright 2017 Pacess Studio.  All rights reserved.
//----------------------------------------------------------------------------------------

header("Access-Control-Allow-Origin: https://www.pacess.com");
header("Access-Control-Allow-Methods: POST");
header("Content-type: text/html");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Tue, 10 Mar 1987 00:00:00 GMT");

date_default_timezone_set("Asia/Hong_Kong");
mb_internal_encoding("UTF-8");
ini_set("memory_limit", "-1");
ini_set("default_socket_timeout", 10);
set_time_limit(0);

session_start();

//----------------------------------------------------------------------------------------
//  * Order is important
require_once "./libraries/vendor/autoload.php";

//----------------------------------------------------------------------------------------
//  Global variables
$_apiName = baseName(__FILE__);

//----------------------------------------------------------------------------------------
//  Check if logout
if (isset($_REQUEST["logout"]))  {
 unset($_SESSION["access_token"]);
 exit(0);
}

//----------------------------------------------------------------------------------------
//  Check if code provided for authentication
$client = new Google_Client();
$client->setAuthConfig("./client_secret.json");
$client->addScope("https://mail.google.com/");
$client->setRedirectUri("https://www.pacess.com/gmailGrabber");
if (isset($_REQUEST["code"]))  {

 $client->authenticate($_REQUEST["code"]);
 $_SESSION["access_token"] = $client->getAccessToken();

 $auth_url = "https://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
 header("Location: ".filter_var($auth_url, FILTER_SANITIZE_URL));
 exit(0);
}

//----------------------------------------------------------------------------------------
//  Check if access token created
if (isset($_SESSION["access_token"]))  {
 $client->setAccessToken($_SESSION["access_token"]);
}  else  {

 //  Not yet
 $auth_url = $client->createAuthUrl();
 header("Location: ".filter_var($auth_url, FILTER_SANITIZE_URL));
 exit(0);
}

//----------------------------------------------------------------------------------------
//  Create Google service
$service = new Google_Service_Gmail($client);

//----------------------------------------------------------------------------------------
//  OK, everything is ready!
try  {
 if (!$client->getAccessToken())  {
  echo("Something happened...");
  exit(0);
 }

 $accessToken = $client->getAccessToken();  
 echo("Access token: ".print_r($accessToken, true)."<hr>");

 $labelsResponse = $service->users_labels->listUsersLabels("me");
 if ($labelsResponse->getLabels()) {
  $labels = $labelsResponse->getLabels();
  foreach ($labels as $label) {
   var_dump($label);
   echo("<hr>");
  }
 }
}  catch (Google_Auth_Exception $e)  {
 echo("### Error...");
}

?>

沒有留言: