gpt4 book ai didi

php - 如何使用存储的刷新 token 获取更新的访问 token

转载 作者:可可西里 更新时间:2023-11-01 12:17:57 25 4
gpt4 key购买 nike

我正在构建一个应用程序,允许管理员验证对其分析帐户的访问以供离线使用,并将刷新 token 存储在数据库中。

现在,当我尝试在前端使用 API 时,它返回以下错误:

"Access Token Expired. There wan a general error : The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved."

到目前为止,这是我生成此错误的代码:

require_once "lib/google/Google_Client.php";
require_once "lib/google/contrib/Google_AnalyticsService.php";

$_analytics = new analytics();
$_googleClient = new Google_Client();
$_googleClient->setClientId($_analytics->gaClientId);
$_googleClient->setClientSecret($_analytics->gaClientSecret);
$_googleClient->setRedirectUri($_analytics->gaRedirectUri);
$_googleClient->setScopes($_analytics->gaScope);
$_googleClient->setAccessType($_analytics->gaAccessType);

// Returns last access token from the database (this works)
$_tokenArray['access_token'] = $_analytics->dbAccessToken($_agencyId);
$_googleClient->setAccessToken(json_encode($_tokenArray));

if($_googleClient->isAccessTokenExpired()) {
// Don't think this is required for Analytics API V3
//$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId));
echo 'Access Token Expired'; // Debug
}

if (!$_googleClient->getAccessToken()) {
echo '<h2>Error - Admin has not setup analytics correct yet</h2>';
}

我正在寻找一个函数来运行类似 setRefreshToken 的东西——从数据库中输入值,从管理员之前在线验证它。

最佳答案

您可以尝试以下操作,您需要添加代码以将新 token 存储在数据库中。

if($_googleClient->isAccessTokenExpired()) {
// Don't think this is required for Analytics API V3
//$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId));
echo 'Access Token Expired'; // Debug

$_googleClient->authenticate();
$NewAccessToken = json_decode($_googleClient->getAccessToken());
$_googleClient->refreshToken($NewAccessToken->refresh_token);
}

关于php - 如何使用存储的刷新 token 获取更新的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15770674/

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