gpt4 book ai didi

oauth-2.0 - 如何获取带有自动批准响应的refresh_token?

转载 作者:行者123 更新时间:2023-12-03 06:48:43 25 4
gpt4 key购买 nike

我有以下代码:

if (isset($_REQUEST['logout']))
{
unset($_SESSION['upload_token ']);
}

if (isset($_GET['code']))
{
$client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['upload_token']) && $_SESSION['upload_token'])
{
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired())
{
echo "The access token is expired.<br>"; // Debug
$client->refreshToken(json_decode($_SESSION['upload_token']));
unset($_SESSION['upload_token']);
}
}
else
{
$authUrl = $client->createAuthUrl();
}

并且收到以下错误:

Uncaught exception 'Google_Auth_Exception' with message '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.'

我假设我收到此错误是因为响应是自动批准的。

应该改变什么?

更新:我尝试将其添加到我的代码中:

$client->setAccessType("online");
$client->setApprovalPrompt("auto");

基于this question 。我仍然收到相同的缺少刷新 token 的错误。

更新:kroikie 更新后,我的代码如下所示:

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setAccessType("offline");
$client->setApprovalPrompt("auto");
$client->setApplicationName("Appraisal App");
$service = new Google_Service_Drive($client);

if (isset($_REQUEST['logout']))
{
unset($_SESSION['upload_token ']);
}

if (isset($_GET['code']))
{
$resp = $client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$array = get_object_vars(json_decode($resp));
// store and use $refreshToken to get new access tokens
$refreshToken = $array['refreshToken'];
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['upload_token']) && $_SESSION['upload_token'])
{
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired())
{
echo "The access token is expired. Let Raph know that you saw this.<br>";
$client->refreshToken($refreshToken);
unset($_SESSION['upload_token']);
}
}
else
{
$authUrl = $client->createAuthUrl();
}

不幸的是,当需要刷新 token 时,我仍然收到相同的 fatal error 。

最佳答案

当访问类型为离线时,当用户首次授予数据访问权限时,会返回访问 token 和刷新 token 。访问 token 可用于访问用户的数据,刷新 token 将被存储并用于在初始访问 token 过期时获取新的访问 token 。

所以尝试使用离线访问类型

$client->setAccessType('offline');

并使用刷新 token 刷新客户端的访问 token

// $refreshToken is retrieved from the response of the
// user's initial granting access
$client->refreshToken($refreshToken)

更新:要获取刷新 token ,请使用以下内容:

if (isset($_GET['code'])) {
$resp = $client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$array = get_object_vars(json_decode($resp));
// store and use $refreshToken to get new access tokens
$refreshToken = $array['refreshToken'];
}

关于oauth-2.0 - 如何获取带有自动批准响应的refresh_token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23940665/

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