首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >刷新oauth2令牌google和HWIOAuthBundle

刷新oauth2令牌google和HWIOAuthBundle
EN

Stack Overflow用户
提问于 2016-11-10 08:48:37
回答 2查看 1.7K关注 0票数 10

我如何刷新令牌?我使用Google与此令牌-它工作,但找不到如何刷新它,在本例中,我们没有节省过期的时间。我要求

代码语言:javascript
复制
`access_type:     offline `

然后

代码语言:javascript
复制
$client = new Google_Client();
        //$client->setClientId($GoogleClientId);
        $client->setApplicationName($GoogleAppName);
        $client->setClientId($this->user->getGoogleId());
        $client->setAccessType('offline');

如果令牌是有效的,我可以工作,但当过期时,我尝试

代码语言:javascript
复制
$token = [
            'access_token' => $this->user->getGoogleAccessToken(),
            'expires_in'   => (new \DateTime())->modify('-1 year')->getTimestamp(),
        ];

我把这个日期放在任何日期,因为在这个例子中我们没有节省过期的时间

https://gist.github.com/danvbe/4476697

代码语言:javascript
复制
    $client->setAccessToken($token);

    if($client->isAccessTokenExpired()){

        $refreshedToken = $client->refreshToken($client->getAccessToken());

这里我有错误

代码语言:javascript
复制
array:2 [▼
  "error" => "invalid_request"
  "error_description" => "Could not determine client ID from request."
]

有HwiAuthBundle方法来刷新令牌吗?为什么这不适用于Google_Client刷新?

EN

回答 2

Stack Overflow用户

发布于 2017-09-20 07:34:08

在oauth2.0中,要刷新过期的访问令牌,需要发送到端点:

  • 授予类型等于“refresh_token”
  • 有效的refreshToken
  • 你的clientId
  • 还有你的clientSecret

您不能发送过期的accessToken来获得一个新的刷新的accessToken.

代码语言:javascript
复制
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;
}

函数refreshAccessToken($refreshToken,..。

而非$accessToken

我认为你需要在用你的凭证构造你的客户之后打电话给你

代码语言:javascript
复制
$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()是什么?我认为您还需要创建一个oauth客户机id:https://developers.google.com/identity/sign-in/web/devconsole-project

oauth client_id中的client_id不是用户id,而是应用程序id。

票数 2
EN

Stack Overflow用户

发布于 2017-09-19 11:31:38

很抱歉打扰您,朋友,但是看起来那个包没有实现任何刷新令牌功能。或者由你自己决定。

下面是他们的GitHub中的公开问题,来看看:https://github.com/hwi/HWIOAuthBundle/issues/457

下面是这个问题的一个评论:

这个特性是存在的,但是它并不容易使用,因为您需要自己完成所有事情(处理存储令牌的更多细节、检测过期信息、调用Google获取新令牌和替换旧令牌),目前只能从这个包中获得帮助,它的代码允许您向Google请求新的令牌: GenericOAuth2ResourceOwner::refreshToken(),它应该按预期工作,但是我很长时间没有使用这个包了=)

那里的人们都在等待Gist (代码片段)来向他们展示如何做到这一点,但到目前为止还没有。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40523329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档