В ключе Json отсутствует поле обновления токена в API фотографий Google - PullRequest
2 голосов
/ 22 апреля 2019

Я создаю приложение, используя Google Photos API в php.Вот мой код

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;
}

Вот ошибка при перенаправлении на аутентификацию

Неустранимая ошибка: Uncaught InvalidArgumentException: в ключе json отсутствует поле refresh_token в C: \ xampp \ htdocs\ gphotos \ vendor \ google \ auth \ src \ Credentials \ UserRefreshCredentials.php: 78 Трассировка стека: # 0 C: \ xampp \ htdocs \ gphotos \ config.php (49): Google \ Auth \ Credentials \ UserRefreshCredentials -> __ construct (Массив, Массив) # 1 C: \ xampp \ htdocs \ gphotos \ index.php (5): connectWithGooglePhotos () # 2 {main}, выброшенный в C: \ xampp \ htdocs \ gphotos \ vendor \ google \ auth \ src \ Credentials\ UserRefreshCredentials.php в строке 78

Будем благодарны за любые решения!

1 Ответ

1 голос
/ 22 апреля 2019

'refreshToken' должно быть 'refresh_token', потому что ключ для токена обновления - refresh_token

, поэтому вам нужно изменить свои учетные данные на

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