How can I set the refresh_token when working with the google api?

前端 未结 1 384
闹比i
闹比i 2021-01-03 12:54

I\'m working off an example trying to learn how to use the google api to change events on a calendar. The server is the user which will update the calendar based off of inf

相关标签:
1条回答
  • 2021-01-03 12:57

    Check if the token has a refresh-token (if you request offline access, the refresh-token will be sent with the access token the first time).

    Something like this

     $token = $client->getAccessToken();
     $authObj = json_decode($token);
     if(isset($authObj->refresh_token)) {
         save_refresh_token($authObj->refresh_token);
     }
    

    Where save_refresh_token saves your refresh-token somewhere (db).

    Then you can check if token as expired by:

     $client->isAccessTokenExpired()
    

    If it is, you can update it with:

    $client->refreshToken($your_saved_refresh_token);
    

    And then set your new access token to the session:

     $_SESSION['access_token'] = $client->getAccessToken();
    
    0 讨论(0)
提交回复
热议问题