1

enter image description here

In the past for days I wasted of my good much time now I managed to get refresh token.. I couldn't get a refresh token before..

My purpose is to synchronize Google calendar data without Google login of users

What I did to do so:

  1. I stored token information in my database
  2. when user wants to synchronize I used the stored information to pull data from Google calendar API but then the token has expiration date, which is an hour
  3. I assume I can get an usable token using refresh token and pull data from Google calendar api again ( but how ? )

extra question : can I get another refresh token from the previous refresh token? Is it like I can get a refresh token over and over again?

Here's some my code:

 public function __construct()
{
    $client = new Google_Client();
    $client->setAuthConfig('client_secret.json');
    $client->addScope(Google_Service_Calendar::CALENDAR); // CALENDAR READONLY 는 읽기만 가능
    $guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
    $client->setHttpClient($guzzleClient);
    $client->setAccessType ("offline");
    $client->setApprovalPrompt ("force");
    $client->setRedirectUri('http://localhost/smart_mirror/GoogleCalendarApi-master/public/api/cal');
    $this->client = $client;
}


$refresh_token=$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken()); 
$this->client->getAuth()->refreshToken($this->client->getAuth()->getRefreshToken()); 
$refresh_token=$this->client->getAccessToken();
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
jh 2
  • 25
  • 1
  • 7

1 Answers1

0

Access tokens have limited lifetimes. If your application needs access to a Google API beyond the lifetime of a single access token, it can obtain a refresh token. A refresh token allows your application to obtain new access tokens.

Note: Save refresh tokens in secure long-term storage and continue to use them as long as they remain valid. Limits apply to the number of refresh tokens that are issued per client-user combination, and per user across all clients, and these limits are different. If your application requests enough refresh tokens to go over one of the limits, older refresh tokens stop working.

Refreshing an access token (offline access)

If your application needs offline access to a Google API, set the API client's access type to offline:

$client->setAccessType("offline");

Here's an SO post an additional sample.

noogui
  • 15,376
  • 3
  • 18
  • 42