Google API Client “refresh token must be passed in or set as part of setAccessToken”

后端 未结 9 1385
Happy的楠姐
Happy的楠姐 2020-12-05 07:55

I am currently facing a very strange problem, indeed I\'ve been following this very same guide (https://developers.google.com/google-apps/calendar/quickstart/php) from Googl

9条回答
  •  甜味超标
    2020-12-05 08:30

    I have faced the same issue with new google api library. Search for solution brought the following link: RefreshToken Not getting send back after I get new token google sheets API

    Based on that information, I have modified the quickstart code part to fit my needs. After first authorization with Google I have obtained drive-php-quickstart.json which contains refresh_token that expires in 3600 seconds or one hour. The refresh token is issued only once, so if it is lost, then re-authorization is required. So, that to have it always in drive-php-quickstart.json I have done following:

    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
    // save refresh token to some variable
    $refreshTokenSaved = $client->getRefreshToken(); 
    
    // update access token
    $client->fetchAccessTokenWithRefreshToken($refreshTokenSaved); 
    
    // pass access token to some variable
    $accessTokenUpdated = $client->getAccessToken();
    
    // append refresh token
    $accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
    
    // save to file
    file_put_contents($credentialsPath, json_encode($accessTokenUpdated)); 
    }
    

提交回复
热议问题