Google カレンダーで 1 つのイベントを作成する PHP スクリプトを作成する必要があります。クライアント ID、クライアント シークレット、開発キーの設定、および新しいイベントの作成に問題はありませんでした。
私の唯一の問題は OAuth2 にあります。特に、永続的な接続を確立する必要があり、スクリプトを実行するたびに認証を行いたくありません。
実際、このスクリプトを使用すると、トークンと更新トークンを取得できますが、1 時間ごとにトークンが期限切れになり、更新方法がわかりません。このコードを編集してそれを行うにはどうすればよいですか? トークンと更新トークンの両方をどこかに保存して、常に同じデータを使用することはできますか?
「OAuth2 トークンの更新中にエラーが発生しました。メッセージ: '{ "error" : "invalid_grant"
このトピックに関する他の投稿をstackoverflowですでに読んでいますが、まだ解決策が見つかりません... :\
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Calendar App");
$client->setClientId('xxxx');
$client->setClientSecret('yyy');
$client->setRedirectUri('http://www.zzzzzz');
$client->setDeveloperKey('kkk');
$client->setAccessType('offline');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
//echo $_SESSION['token'];
//$client->setAccessToken($_SESSION['token']);
$authObj = json_decode($_SESSION['token']);
$accessToken = $authObj->access_token;
$refreshToken = $authObj->refresh_token;
$tokenType = $authObj->token_type;
$expiresIn = $authObj->expires_in;
echo 'access_token = ' . $accessToken;
echo '<br />';
echo 'refresh_token = ' . $refreshToken;
echo '<br />';
echo 'token_type = ' . $tokenType;
echo '<br />';
echo 'expires_in = ' . $expiresIn;
}
if(!empty($cookie)){
$client->refreshToken($this->Cookie->read('token'));
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Creation of a single event
$event = new Google_Event();
$event->setSummary($event_name);
$event->setLocation('');
....
?>
協力してくれてありがとうございます!