简体   繁体   English

刷新oauth2令牌google api和HWIOAuthBundle

[英]Refresh oauth2 token google api and HWIOAuthBundle

How i can refresh token ? 我怎么刷新令牌? I use Google api with this token - it work but can't find how to refresh it, in this example we dont save expired time. 我使用带有此令牌的Google api - 它可以工作但无法找到如何刷新它,在这个例子中我们不保存过期时间。 I require 我需要

`access_type:     offline `

then 然后

$client = new Google_Client();
        //$client->setClientId($GoogleClientId);
        $client->setApplicationName($GoogleAppName);
        $client->setClientId($this->user->getGoogleId());
        $client->setAccessType('offline');

if token is valid i can work but when is expired i try 如果令牌有效,我可以工作但是什么时候过期我试试

$token = [
            'access_token' => $this->user->getGoogleAccessToken(),
            'expires_in'   => (new \DateTime())->modify('-1 year')->getTimestamp(),
        ];

i put this any date because in this example we don't save expired time 我把它放在任何日期,因为在这个例子中我们不保存过期时间

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

    $client->setAccessToken($token);

    if($client->isAccessTokenExpired()){

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

here i have error 我有错误

array:2 [▼
  "error" => "invalid_request"
  "error_description" => "Could not determine client ID from request."
]

There is HwiAuthBundle method to refresh token ? 有HwiAuthBundle方法来刷新令牌吗? Why this not work with Google_Client refresh ? 为什么这不适用于Google_Client刷新?

In oauth2.0 to refresh an expired access token you need to send to the endpoint : 在oauth2.0中刷新过期的访问令牌,您需要发送到端点:

  • a grant type equals to 'refresh_token' 授权类型等于'refresh_token'
  • a valid refreshToken 一个有效的refreshToken
  • your clientId 你的clientId
  • and your clientSecret 和你的clientSecret

You can't send an expired accessToken to get a new refreshed accessToken. 您无法发送过期的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 , ... function refreshAccessToken( $ refreshToken ,...

and not $accessToken 而不是$ accessToken

I think you need to call after construct your client with your credentials 我认为您需要在使用凭据构建客户端后调用

$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 https://developers.google.com/api-client-library/php/auth/web-app#creatingcred

Are you sure of your $client->setClientId($this->user->getGoogleId()); 你确定你的$client->setClientId($this->user->getGoogleId()); ? What is getGoogleId() ? 什么是getGoogleId()? I think you need also to create a oauth client id : https://developers.google.com/identity/sign-in/web/devconsole-project 我认为您还需要创建一个oauth客户端ID: https//developers.google.com/identity/sign-in/web/devconsole-project

In oauth client_id is not the user id but the app id 在oauth中,client_id不是用户ID,而是app id

Sorry to upset you amigo, but it looks like that package doesn't implement any Refresh Token functionality. 很抱歉打扰你的amigo,但看起来该软件包没有实现任何刷新令牌功能。 Or it's left up to you. 或者它留给你。

Here's the open issue in their GitHub, have a look: https://github.com/hwi/HWIOAuthBundle/issues/457 这是他们的GitHub中的开放性问题,看看: https//github.com/hwi/HWIOAuthBundle/issues/457

Here's a comment from the issue: 以下是该问题的评论:

This feature exists, yet there is no easy use for it as you need to do everything on your own (dealing with storing more details about token, detecting the expiration, calling Google to get new token, and replacing old), only help from this bundle for now, it's code that allows you to ask Google for new fresh token: GenericOAuth2ResourceOwner::refreshToken(), it should work as expected, but I have not used this bundle for long time =) 这个功能存在,但它没有简单的用途,因为你需要自己做所有事情(处理存储更多关于令牌的细节,检测到期,调用谷歌获取新令牌,以及更换旧),只有这个的帮助现在捆绑,它的代码允许你向谷歌询问新的新令牌:GenericOAuth2ResourceOwner :: refreshToken(),它应该按预期工作,但我没有长时间使用这个包=)

People in there are waiting on a Gist (snippet of code) to show them how to do this, but so far nothing. 那里的人正在等待Gist(代码片段)向他们展示如何做到这一点,但到目前为止还没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM