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

后端 未结 9 1367
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:42

    I had the same issues and finally go this to work:

    Backstory:

    I received the same error. Here is what I found:

    This error:

    PHP Fatal error: Uncaught LogicException: refresh token must be passed in or set as part of setAccessToken in /Library/WebServer/Documents/Sites/test/scripts/vendor/google/apiclient/src/Google/Client.php:267

    Is referencing the update access token (aka Refresh) method:

    $client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
    

    Why was it failing? Long story short, I realized when I printed out the $accessToken array, which comes from decoding this json file (per the quickstart code you posted/that comes from google)

    credentials/calendar-php-quickstart.json

    I found the error due to how the accessToken array prints out when I print_r:

    Array ( [access_token] => Array ( [access_token] => xyz123 [token_type] => Bearer [expires_in] => 3600 [refresh_token] => xsss112222 [created] => 1511379484 )

    )

    Solution:

    $refreshToken = $accessToken["access_token"]["refresh_token"];

    right before this line:

        $client->fetchAccessTokenWithRefreshToken($refreshToken);
    

    I can finally refresh the token as needed when it expires in an hour. I think the devs of this article assumed the array prints as:

    Array ( [access_token] => xyz123 [token_type] => Bearer [expires_in] => 3600 [refresh_token] => xsss112222 [created] => 1511379484 )

    so they thought you could simply do $accessToken["refresh_token"]; That is not correct.

    Now we have a valid value for $refreshToken, so the error should go away if you do this. I also updated the author via the feedback link to let them know about this, in case other php devs encounter this issue. Hopefully this helps somebody. My apologies if I formatted this post poorly I am new to S.E. I just wanted to share as I finally got this to work.

    0 讨论(0)
  • 2020-12-05 08:43

    Google has updated their PHP Quickstart, with an improved method to handle this:

    Snippet below:

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
    $client->setAccessToken($accessToken);
    
    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }
    
    0 讨论(0)
  • 2020-12-05 08:43

    In my case I had forgotten to set the access type as "offline" without which the refresh token was not being generated.

    $client->setAccessType('offline');
    

    Once this is done, the sample code given in the google documentation will work.

    // Exchange authorization code for an access token.
    // "refresh_token" is returned along with the access token
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
    $client->setAccessToken($accessToken);
    
    
    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }
    
    0 讨论(0)
提交回复
热议问题