如何刷新令牌?我使用谷歌api与这个令牌-它的工作,但无法找到如何刷新它,在这个例子中,我们不保存过期时间。我需要
`access_type: offline `
然后$client = new Google_Client();
//$client->setClientId($GoogleClientId);
$client->setApplicationName($GoogleAppName);
$client->setClientId($this->user->getGoogleId());
$client->setAccessType('offline');
如果令牌有效,我可以工作,但当它过期时,我尝试
$token = [
'access_token' => $this->user->getGoogleAccessToken(),
'expires_in' => (new DateTime())->modify('-1 year')->getTimestamp(),
];
我输入了任意日期因为在这个例子中我们不保存过期时间
https://gist.github.com/danvbe/4476697 $client->setAccessToken($token);
if($client->isAccessTokenExpired()){
$refreshedToken = $client->refreshToken($client->getAccessToken());
这里有错误
array:2 [▼
"error" => "invalid_request"
"error_description" => "Could not determine client ID from request."
]
有HwiAuthBundle方法刷新令牌?为什么这不能与Google_Client刷新?
在oauth2.0中刷新过期的访问令牌需要发送到端点:
- 一个授权类型等于'refresh_token'
- 一个有效的refreshToken
- 你的客户端id
- 和你的clientSecret
你不能发送一个过期的accessToken来获得一个新的刷新的accessToken。
public function refreshAccessToken($refreshToken, array $extraParameters = array())
{
$parameters = array_merge(array(
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
'client_id' => $this->options['client_id'],
'client_secret' => $this->options['client_secret'],
), $extraParameters);
$response = $this->doGetTokenRequest($this->options['access_token_url'], $parameters);
$response = $this->getResponseContent($response);
$this->validateResponseContent($response);
return $response;
}
function refreshAccessToken($refreshToken,…
而非$accessToken
我认为您需要在使用您的凭据构建您的客户端后调用
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->refreshToken($client->getRefreshToken());
https://developers.google.com/api-client-library/php/auth/web-app creatingcred
你确定你的$client->setClientId($this->user->getGoogleId());
吗?什么是getGoogleId() ?我认为你还需要创建一个合法的客户端id:https://developers.google.com/identity/sign-in/web/devconsole-project
在oauth中client_id不是用户id,而是应用id
抱歉让你不高兴了,但是看起来这个包没有实现任何刷新令牌功能。或者你自己决定。
这是他们GitHub中的开放问题,看看:https://github.com/hwi/HWIOAuthBundle/issues/457
以下是该问题的评论:
这个特性是存在的,但是不像你需要的那样容易使用你可以自己处理所有事情(处理存储更多关于令牌的细节,检测过期,调用Google获取新的令牌,以及取代旧的),现在只能从这个包中获得帮助,它的代码是允许您向Google请求新的令牌:GenericOAuth2ResourceOwner::refreshToken(),它应该作为预料之中,但是我很久没有使用这个包了=)
那里的人们正在等待一个Gist(代码片段)来告诉他们如何做到这一点,但到目前为止还没有。