gpt4 book ai didi

php - Json key 缺少 google photos api 中的刷新 token 字段

转载 作者:行者123 更新时间:2023-12-05 05:11:08 24 4
gpt4 key购买 nike

我正在 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;
}

这是重定向验证时的错误

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

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

最佳答案

'refreshToken' 需要是 'refresh_token' 因为刷新 token 的键是 refresh_token

因此您需要将您的凭据更改为

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

关于php - Json key 缺少 google photos api 中的刷新 token 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55790419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com