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

后端 未结 9 1382
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.

提交回复
热议问题