Calendar Service を使用するために PHP 用の Google 標準ライブラリを使用しており、Google API コンソールを介して OAuth 2.0 認証用のサービス アカウント タイプを設定しています。
私の主な目的は、ユーザーの Google カレンダー (例: user@organisationname.com) (ユーザーがオンラインでない場合) をバッチで更新することです。例えば。ユーザーの予定表のイベントを更新します。
ユーザーが (OAuth2.0 を使用して) アプリケーションにログインすると、アプリケーションが「カレンダーを管理する」、「カレンダーを表示する」、および「アプリケーションを使用していないときにこれらの操作を実行する」権限を付与します。
次のコードは、OAuth2.0 を使用してログインするために使用されます。
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('XXXXX-flue2a9o5ll602ovrhaejlpm9otgjh1r.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXX');
$client->setRedirectUri('http://localhost/testAPI/google-api-php-client/examples/calendar/simple.php');
$client->setDeveloperKey('AIzaSyCGvXRXGMo58ZDswyb4zBkJgRMLcHBRIrI');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['code']=$_GET['code'];
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
echo $_SESSION['code'];
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
アクセス許可を取得したら、ユーザーがログインしていないときに、将来これらのアクセス許可を使用するために何かを保存する必要がありますか?
次のコードは、ユーザーがログインしているときに正常に動作します。ただし、ユーザーがログアウトすると、Error refresh the OAuth2 token, message: '{ "error" : "access_denied" }'が返されます
<?php
require_once '../src/Google_Client.php';
require_once '../src/contrib/Google_CalendarService.php';
session_start();
const CLIENT_ID = 'XXXXXX.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'XXXX@developer.gserviceaccount.com';
const KEY_FILE = 'f183b8caXXXXXXXXatekey.p12';
$client = new Google_Client();
$client->setApplicationName("XXXXXXXX Calendar Service");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key,
'notasecret',
'http://oauth.net/grant_type/jwt/1.0/bearer',
'363183053@developer.gserviceaccount.com')
);
$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
try{
$cal->events->quickAdd("info@organisationname.com", "SERVICE TEST ");
}catch(Exception $e){
print_r($e->getMessage());
}
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
echo $_SESSION['token'] = $client->getAccessToken();
}
ユーザーがログインしていないときにカレンダーを更新するにはどうすればよいですか(ユーザーが許可を与えている場合)。ユーザーがログインしたときにアクセス コードを保存し、後でバッチを実行するときにそれを使用する必要がありますか?
ところで、関連付けハンドルとは何ですか?