user9312196
user9312196

Reputation:

Json key is missing the refresh token field in google photos api

I am creating a app using google photos api in php. Here is my code

function connectWithGooglePhotos()
{
$clientSecretJson = json_decode(
    file_get_contents('credentials.json'),
    true
)['web'];
$clientId = $clientSecretJson['client_id'];
$clientSecret = $clientSecretJson['client_secret'];
$tokenUri = $clientSecretJson['token_uri'];
$redirectUri = $clientSecretJson['redirect_uris'][0];
$scopes = ['https://www.googleapis.com/auth/photoslibrary'];

$oauth2 = new OAuth2([
    'clientId' => $clientId,
    'clientSecret' => $clientSecret,
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'redirectUri' => $redirectUri,
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'scope' => $scopes,
]);

// The authorization URI will, upon redirecting, return a parameter called code.
if (!isset($_GET['code'])) {
    $authenticationUrl = $oauth2->buildFullAuthorizationUri(['access_type' => 'offline']);
    header('Location: ' . $authenticationUrl);
} else {
    // With the code returned by the OAuth flow, we can retrieve the refresh token.
    $oauth2->setCode($_GET['code']);
    $authToken = $oauth2->fetchAuthToken();
    $refreshToken = $authToken['access_token'];

    // The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when
    // they expire.
    $_SESSION['credentials'] = new UserRefreshCredentials(
        $scopes,
        [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'refreshToken' => $refreshToken,
        ]
    );

    $photosLibraryClient = new PhotosLibraryClient(['credentials' => $_SESSION['credentials']]);
}

return $photosLibraryClient;
}

Here is the error while redirect to authenticate

Fatal error: Uncaught InvalidArgumentException: json key is missing the refresh_token field in C:\xampp\htdocs\gphotos\vendor\google\auth\src\Credentials\UserRefreshCredentials.php:78 Stack trace: #0 C:\xampp\htdocs\gphotos\config.php(49): Google\Auth\Credentials\UserRefreshCredentials->__construct(Array, Array) #1 C:\xampp\htdocs\gphotos\index.php(5): connectWithGooglePhotos() #2 {main} thrown in C:\xampp\htdocs\gphotos\vendor\google\auth\src\Credentials\UserRefreshCredentials.php on line 78

Any solutions will be appreciated !

Upvotes: 3

Views: 3100

Answers (2)

Rajitha Wijayaratne
Rajitha Wijayaratne

Reputation: 726

Make sure 'prompt' setting is set to 'consent' as shown below.

$url = $oauth2->buildFullAuthorizationUri([
    'prompt' => 'consent',
    'access_type' => 'offline'
]);

Normally, only the first invocation of the prompt sends back the refresh token. This is by design.

However, if you need to force consent again use the above setting. This will re-generate the refresh token as well.

Upvotes: 0

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

'refreshToken' needs to be 'refresh_token' because the key for refresh token is refresh_token

so you need to change your credentials to

[
   'client_id' => $clientId,
   'client_secret' => $clientSecret,
   'refresh_token' => $refreshToken,
]

Upvotes: 2

Related Questions