Google OAuth: can't get refresh token with authorization code

前端 未结 2 1393
故里飘歌
故里飘歌 2021-01-23 03:59

I am using Google API Client for Google Analytics with OAuth 2.0

I read this to get the refresh token but it doesn\'t appears: https://developers.google.com/identity/pro

2条回答
  •  余生分开走
    2021-01-23 04:21

    Check out this response by @curious_coder Google API Refresh Token

    He explains a way to get the Refresh Token every time :) Life saver. This post helped me get there so I figured I'd share this for any one else trying to find their refresh token. Add the last two lines of this code to your Auth process

    $client = new Google_Client();
    $client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);    
    $client->setAccessType('offline'); //To fetch refresh token(Refresh token issued only first time)
    $client->setApprovalPrompt('force'); //Adding this will force for refresh token
    

    Also I used var_dump($tokens) to get the contents instead of echo. Dont remember if it makes a difference in PHP but $tokens will be an array of objects.

提交回复
热议问题