【问题标题】:Json key is missing the refresh token field in google photos apiJson 密钥缺少谷歌照片 api 中的刷新令牌字段
【发布时间】:2019-04-22 07:00:12
【问题描述】:

我正在使用 php 中的 google photos api 创建一个应用程序。这是我的代码

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

这是重定向到身份验证时的错误

致命错误:未捕获的 InvalidArgumentException:json 键丢失 C:\xampp\htdocs\gphotos\vendor\google\auth\src\Credentials\UserRefreshCredentials.php:78 中的 refresh_token 字段 堆栈跟踪:#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} 在 C:\xampp\htdocs\gphotos\ vendor\google\auth\src\Credentials\UserRefreshCredentials.php 在第 78 行

任何解决方案将不胜感激!

【问题讨论】:

  • 罗宾,检查我的答案。如果您仍然遇到任何问题,请告诉我

标签: php google-photos google-photos-api


【解决方案1】:

'refreshToken' 必须是 'refresh_token',因为刷新令牌的键是 refresh_token

因此您需要将凭据更改为

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

【讨论】:

  • Danyal,您能否再次检查代码并告诉我为什么刷新令牌不起作用。它会在 1 小时后过期,并向我显示令牌已撤销等错误
  • 您遇到了什么异常?
  • 现在,我不在我的电脑上,但它说你的令牌已过期或被撤销
  • { "error": "invalid_grant", "error_description": "令牌已过期或撤销。" }
  • 表示刷新令牌已过期。你需要重新生成它
猜你喜欢
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-25
  • 2021-05-06
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多