Google api refresh_token null以及如何刷新访问令牌 [英] Google api refresh_token null and how to refresh access token
问题描述
我使用HWIO Bundle进行谷歌API,当我有谷歌响应refreshToken = null为什么?如何刷新令牌
oAuthToken = {HWI \ Bundle \OAuthBundle \Security\Core\Authentication\Token \OAuthToken} [11]
的accessToken = ya29.Ci8lA1JTu9CB81dOFy-nzszViRgCI2CvvKVrCd0Lq-8I0QR_dIrl-_7RccdGt1Islg
rawToken = {}阵列[4]
的access_token =ya29.Ci8lA1JTu9CB81dOFy-nzszViRgCI2CvvKVrCd0Lq-8I0QR_dIrl -_7RccdGt1Islg
token_type =持票人
expires_in = 3578
id_token =xxxxx
refreshToken = null
expiresIn = 3578
createdAt = 1468957368
tokenSecret = null
resourceOwnerName = null
因为在google / apiclient中,version :1.1.7in function need refresh_token
public function getRefreshToken()
{
if (array_key_exists('refresh_token',$ this-> token)){
return $ this-> token ['refresh_token'];
} else {
return null;
}
}
这是我的访问令牌
{access_token:ya29.Ci8lA1JTu9CB81dOFy-nzszViRgCI2CvvKVrCd0Lq-8I0QR_dIrl-_7RccdGt1Islg,token_type:承载,expires_in:3578,id_token: xxxx,created:1468957368}
没有刷新标记,因为从google获取refreshToken = null或需要使用键刷新标记设置null或者这个dosn't metter?
$ isExpired = $ client-> isAccessTokenExpired(); // true(bool在access_token过期时返回True。)
$ refresh = $ client-> getRefreshToken(); // null,因为不是gahe刷新令牌
$ client-> getGoogleClient() - > setAccessType(offline); // some recomendation
$ client-> getGoogleClient() - > setApprovalPrompt(force); // some recomendation
$ isAgainExpired = $ client-> isAccessTokenExpired(); //仍然为true(过期)
仍然有异常 - OAuth 2.0访问令牌已过期,并且刷新令牌不可用。刷新令牌不会返回自动批准的响应。
如何刷新令牌以及如何使用令牌获取刷新令牌,以便刷新令牌?
我尝试 我从HWIO bundle获取访问令牌:
$ client = new Google_Client()
,但在包装器中,在构造函数中。
$ b
hwi_oauth:
$ p $
connect:
account_connector:app.provider.user_provider
firewall_name:secured_area
resource_owners:
google:
类型:google
client_id:xxx.apps.googleusercontent.com
client_secret:xxx
范围:https://www.googleapis.com/ auth / userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive
在获得访问令牌后,我将其设置为DB。然后在Action中为Google api(新的Google Client())创建封装,并从数据库中为此客户端设置我的访问令牌。如何刷新我的访问令牌?我尝试使用函数google api lib setAccessType和setApprovalPrompt,但不是efect
public function __construct(array $ config,LoggerInterface $ symfonyLogger = null)
{
//如果对象应该由服务类返回,则返回true。
//如果返回关联数组,则返回false(默认行为)。
$ config ['use_objects'] = true;
$ client = new \Google_Client($ config);
if($ symfonyLogger){
// BC for Google API 1.0
if(class_exists('\Google_Logger_Psr')){
$ googleLogger = new \Google_Logger_Psr($客户端,$ symfonyLogger);
$ client-> setLogger($ googleLogger);
} else {
$ client-> setLogger($ symfonyLogger);
}
}
$ client - > setApplicationName($配置[ APPLICATION_NAME]);
$ client - > SETCLIENTID($配置[ OAUTH2_CLIENT_ID]);
$ client - > setClientSecret($配置[ OAUTH2_CLIENT_SECRET]);
$ client - > setRedirectUri($配置[ oauth2_redirect_uri]);
$ client - > setDeveloperKey($配置[ Developer_Key开发]);
$ client - > setAccessType($ config ['access_type']);
$ client - > setApprovalPrompt($ config ['approval_prompt']);
$ this - > client = $ client;
config这是:
happy_r_google_api:
application_name:carbon-quanta-137312
oauth2_client_id:xxxx.apps.googleusercontent.com
oauth2_client_secret:xxx
oauth2_redirect_uri :null
developer_key:null
site_name:aog.local.com
access_type:offline
approval_prompt:force
如果我在动作中设置了Google Client,那么这些参数是相同的,如果我添加constructorI,那么我做错了什么?
refresh_token 仅在第一个请求中返回。 当您第二次刷新访问令牌时,它会返回除 refresh_token 以外的所有内容, file_put_contents 会在第二次发生此情况时移除 refresh_token 。
如下修改代码会将原始访问令牌与新的访问令牌合并(请参阅:array_merge)。通过这种方式,您将可以保留您的 refresh_token 用于将来的请求。我向Google提交了以下修补程序,希望在某个时候更新它。
//刷新标记if它已过期。
if($ client-> isAccessTokenExpired()){
$ client-> fetchAccessTokenWithRefreshToken($ client-> getRefreshToken());
$ newAccessToken = $ client-> getAccessToken();
$ accessToken = array_merge($ accessToken,$ newAccessToken);
file_put_contents($ credentialsPath,json_encode($ accessToken));
}
I use HWIO Bundle for google api and when I have response from google refreshToken = null why? How to refresh token
oAuthToken = {HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken} [11] accessToken = "ya29.Ci8lA1JTu9CB81dOFy-nzszViRgCI2CvvKVrCd0Lq-8I0QR_dIrl-_7RccdGt1Islg" rawToken = {array} [4] access_token = "ya29.Ci8lA1JTu9CB81dOFy-nzszViRgCI2CvvKVrCd0Lq-8I0QR_dIrl-_7RccdGt1Islg" token_type = "Bearer" expires_in = 3578 id_token = "xxxxx" refreshToken = null expiresIn = 3578 createdAt = 1468957368 tokenSecret = null resourceOwnerName = null
because in google/apiclient, "version": "1.1.7" in function need refresh_token
public function getRefreshToken() { if (array_key_exists('refresh_token', $this->token)) { return $this->token['refresh_token']; } else { return null; } }
this my access token
{"access_token":"ya29.Ci8lA1JTu9CB81dOFy-nzszViRgCI2CvvKVrCd0Lq-8I0QR_dIrl-_7RccdGt1Islg","token_type":"Bearer","expires_in":3578,"id_token":"xxxx","created":1468957368}
not have refresh token because from google get refreshToken = null or need set null with key refresh token or this dosn't metter ?
$isExpired = $client->isAccessTokenExpired(); // true (bool Returns True if the access_token is expired.) $refresh = $client->getRefreshToken(); //null because not gahe refresh token $client->getGoogleClient()->setAccessType ("offline"); //some recomendation $client->getGoogleClient()->setApprovalPrompt ("force"); //some recomendation $isAgainExpired = $client->isAccessTokenExpired(); // still true (expired)
still have exception -
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.
how to refresh token and how to with token get refresh token, for refresh token ?
I try
- In my code too
$client = new Google_Client()
but in wrapper, in constructor. I get access token from HWIO bundle:
hwi_oauth: connect: account_connector: app.provider.user_provider firewall_name: secured_area resource_owners: google: type: google client_id: xxx.apps.googleusercontent.com client_secret: xxx scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive"
and after I get access token I set it in DB. Then in Action I create wrapper for google api (new Google Client ()) and set this client my accesss token from DB. How to refresh my access token? I try in action use function google api lib setAccessType and setApprovalPrompt, but not efect
public function __construct(array $config, LoggerInterface $symfonyLogger = null)
{
// True if objects should be returned by the service classes.
// False if associative arrays should be returned (default behavior).
$config['use_objects'] = true;
$client = new \Google_Client($config);
if ($symfonyLogger) {
//BC for Google API 1.0
if (class_exists('\Google_Logger_Psr')) {
$googleLogger = new \Google_Logger_Psr($client, $symfonyLogger);
$client->setLogger($googleLogger);
} else {
$client->setLogger($symfonyLogger);
}
}
$client -> setApplicationName($config['application_name']);
$client -> setClientId($config['oauth2_client_id']);
$client -> setClientSecret($config['oauth2_client_secret']);
$client -> setRedirectUri($config['oauth2_redirect_uri']);
$client -> setDeveloperKey($config['developer_key']);
$client -> setAccessType ($config['access_type']);
$client -> setApprovalPrompt ($config['approval_prompt']);
$this -> client = $client;
}
config this is:
happy_r_google_api:
application_name: carbon-quanta-137312
oauth2_client_id: xxxx.apps.googleusercontent.com
oauth2_client_secret: xxx
oauth2_redirect_uri: null
developer_key: null
site_name: aog.local.com
access_type: offline
approval_prompt: force
and if in action I set to Google Client some parameters this is same if I add constructorI, so what I'am doing wrong ?
The refresh_token is only returned on the first request. When you refresh the access token a second time it returns everything except the refresh_token and the file_put_contents removes the refresh_token when this happens the second time.
Modifying the code as following will merge in the original access token with the new one (see: array_merge). This way you will be able to preserve your refresh_token for future requests. I have submitted the following fix to Google, hope they update it at some point.
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$newAccessToken = $client->getAccessToken();
$accessToken = array_merge($accessToken, $newAccessToken);
file_put_contents($credentialsPath, json_encode($accessToken));
}
这篇关于Google api refresh_token null以及如何刷新访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!