【发布时间】:2016-11-22 20:53:35
【问题描述】:
我将 HWIO Bundle 用于 google api,当我收到来自 google refreshToken = null 的响应时,为什么?如何刷新令牌
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
因为在google/apiclient中,函数中的“version”:“1.1.7”需要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":"Bearer","expires_in":3578,"id_token":"xxxx","created":1468957368}
没有刷新令牌,因为从 google 获取 refreshToken = null 或者需要使用密钥刷新令牌设置 null 或者这不符合?
$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)
仍有异常 - 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 = new Google_Client(),但在包装器中,在构造器中。 -
我从 HWIO 包获得访问令牌:
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"
获得访问令牌后,我将其设置在数据库中。然后在行动中,我为 google api(新的 Google 客户端())创建了包装器,并将这个客户端设置为来自 DB 的访问令牌。如何刷新我的访问令牌?我尝试使用函数 google api lib setAccessType 和 setApprovalPrompt,但没有效果
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;
}
配置如下:
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 客户端设置了一些参数,如果我添加构造函数I,这也是相同的,那么我做错了什么?
【问题讨论】:
标签: php google-drive-api google-api-php-client