2017-05-11 2 views
0

Google APIでrefreshTokenプロシージャに問題が発生した場合、期限切れのトークンが発生すると、refreshTokenはAccessTokenを取得しません。私はすでにリフレッシュトークンをこの時点でdbに保存しました。PHP Google APIリフレッシュトークン

$client = new Google_Client(); 
$client->setApplicationName("APP NAME"); 
$client->setClientId($client_id); 
$client->setClientSecret($client_secret); 
$client->setAccessType('offline'); 
$client->setScopes(array('https://www.googleapis.com/auth/calendar')); 
$client->setAccessToken(json_encode($access_token_from_db)); 

if ($client->isAccessTokenExpired()) : 
    $client->refreshToken($refresh_token_from_db); 
    $new_access_token = $client->getAccessToken(); 
    print_r($access_token); 
    //save to db -- but this fails 
endif; 

これを実行すると、新しいアクセストークンはまだ空です。期限切れをチェックする前に、今すぐ期限切れのトークンにsetAccessTokenを設定しても、同じ問題が解決されません。

refreshTokenが既に保存された後の状態では、まっすぐな例はないようです。

私のアプリケーションでは、(アプリケーションの目的を理解するために必要な場合)アクションに基づいてユーザのカレンダーに更新を送信するためにcronジョブを使用します。

この答えは私には正しいようではありません理由: How to refresh token with Google API client?

はそれが$クライアント - > isAccessTokenExpired()メソッドを使用していない、プラスクリーンなソリューションではないですので、スクリプトは非常にあるべきですタスクを完了するために最小限と小さな。

期限が切れた場合(機能を経由して)承認
  • に与えられたものにsetAccessToken
    • 、トークンを保存している間

    を続ける

  • 新しいトークンにアクセスするためにリフレッシュを使用します。理論的には上記の例は、2013年の回答と同様の機能を持っていますが、これは私が信じる最良の方法ではありません。

  • +0

    が重複する可能性[GoogleのAPIクライアントでトークンをリフレッシュするには?](http://stackoverflow.com/questions/9241213/how-to-refresh-token -with-google-api-client) – DaImTo

    +0

    その答えは古く、isAccessTokenExpiredメソッドの代わりにタイミングを使用しており、複雑な応答であるため、より洗練されたソリューション/方法が可能でなければなりません。 –

    +0

    可能な複製は、リフレッシュトークンが期限切れだとも考えていますが、それは想定されていません。 –

    答えて

    0

    私は自分のclientIDを台無しにしていたので、失敗しましたが、私は小規模で効率的かつ効果的な正解を見つけるために永遠にこのスクリプトを投稿しています(以下は厳密にトークンすでに)あなたのDBに保存されているリフレッシュ:

    $client = new Google_Client(); 
    $client->setApplicationName('APP NAME'); 
    $client->setClientId($client_id); 
    $client->setClientSecret($client_secret); 
    $client->setAccessType('offline'); 
    $client->setScopes(array('https://www.googleapis.com/auth/calendar')); 
    // my app is for google calendar. 
    
    $access_token_from_db = 'XXXXXX'; 
    $refresh_token_from_db = 'XXXXX'; 
    $_tokenArray['access_token'] = $access_token_from_db 
    
    //must be set as json 
    $client->setAccessToken(json_encode($_tokenArray)); 
    
    //check if token expired: 
    if ($client->isAccessTokenExpired()) : 
        $client->refreshToken($refresh_token_from_db); 
        $new_access_token = $client->getAccessToken(); 
    
        //now save your new access token to your db 
    
    endif; 
    
    関連する問題