3

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('');                    
....

?>

協力してくれてありがとうございます!

4

3 に答える 3

5

このページhttps://developers.google.com/accounts/docs/OAuth2WebServer#offlineでは、更新トークンの仕組みと、それを使用して生の http を使用して新しいアクセス トークンを取得する方法について説明しています。

この質問からHow to refresh token with Google API client? ここに同等のphpがあります

here is the snippet to set token, before that make sure the access type should be set to offline

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['access_token'] = $client->getAccessToken();
}
To refresh token

$google_token= json_decode($_SESSION['access_token']);
$client->refreshToken($google_token->refresh_token);
this will refresh your token, you have to update it in session for that you can do

 $_SESSION['access_token']= $client->getAccessToken()

デバッグ 次の 3 つの手順に従って、oauth アプリケーションをデバッグします

  1. https://developers.google.com/accounts/docs/OAuth2と、使用している oauth のフレーバーに応じて適切なサブページ (webapp、javascript など)を読んでいることを確認してください。アプリが何をしているのかを理解していなければ、アプリのデバッグをうまく進めることはできません。

  2. https://developers.google.com/oauthplayground/の Oauth Playground を使用し て、Oauth の手順を実行し、最終的に Google API 呼び出しを行います。これにより、理解が確認され、http 呼び出しと応答がどのように見えるかがわかります。

  3. トレース/ロギング/プロキシなどを使用して、アプリからの実際の http トラフィックをトレースします。これをステップ 2 で観察したものと比較してください。これにより、問題がどこにあるのかがわかります。

于 2013-11-08T06:32:16.597 に答える
2

2回認証しようとしていたため、以前にこのエラーが発生しました。

この行があるので:

if (isset($_GET['code']))

既に認証されているかどうかに関係なく、コードがクエリ文字列にある場合は認証を試みます。(再) 認証を試みる前に、SESSION トークンが存在するかどうかを確認してください。

于 2013-12-09T23:45:46.273 に答える
0

setDeveloperKey() を削除してみてください。これは私にとっていくつかの問題を引き起こしており、不要なようです。

于 2014-06-18T23:58:22.900 に答える