使用刷新令牌更新后访问令牌相同
Access token is same after renew with refresh token
我在 Youtube 上 1 小时后遇到 0auth 问题已经被封锁了两天 api。 401 凭据错误。
$OAUTH2_CLIENT_ID = 'XXXXX';
$OAUTH2_CLIENT_SECRET = 'XXXXX';
$client = new Google_Client();
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$service = new Google_Service_YouTube($client);
if (isset($_GET['code'])){
$accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($accessToken);
$test=$client->getAccessToken();
//TEST REFRESH TOKEN
print_r($test);
sleep(10);
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$test=$client->getAccessToken();
print_r($test);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
...
foreach ($files as $file){
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
} // end if token expired
====CALL YOUTUBE API HERE IN FOREACH LOOP ===
}// end foreach files
}
我的测试代码结果显示访问令牌在提供刷新令牌后根本没有改变,甚至过期时间也没有减少'expires_in'
这就是为什么我在一个小时后遇到凭据错误...我不知道我的代码有什么问题,请帮助我。
这是我在获取访问代码后的测试代码的结果,所以你可以看到 'new' 访问令牌与前一个类似,我已经尝试在参数上也使用 encode_json
setAccessToken()
和 fetchAccessTokenWithRefreshToken()。没有收到错误,但结果仍然相同...
Array ( [access_token] => ya29.GlvQBuGBfZDQn3E8HWd4wfSbb0hLHsYVGzPBE0boJuB4ien5pcsOGqXlkEyOU7mevDLOGOWbuakTyTiAUVf2bkxNwZXX [expires_in] => 3600 [refresh_token] => 1/KEgjy2t9kTNwCXk-ZtMTSzPSS2xl4XX [scope] => https://www.googleapis.com/auth/youtube [token_type] => Bearer [created] => 1552891085 )
Array ( [access_token] => ya29.GlvQBuGBfZDQn3E8HWd4wfSbb0hLHsYVGzPBE0boJuB4ien5pcsOGqXlkEyOU7mevDLOGOWbuakTyTiAUVf2bkxNwZXX [expires_in] => 3600 [refresh_token] => 1/KEgjy2t9kTNwCXk-ZtMTSzPSS2xl4XX [scope] => https://www.googleapis.com/auth/youtube [token_type] => Bearer [created] => 1552891085 )
谢谢
访问令牌在一小时后过期,这就是它们的工作方式。访问令牌过期后,您应该 运行 您的代码并获取新的访问令牌。在新的访问令牌过期之前获取新的访问令牌将导致相同的访问令牌。
您的访问令牌是在创建时创建的(提示 epoch converter)添加 3600(秒)以了解它何时过期。
1552891085 <--- Monday, March 18, 2019 6:38:05 AM
我唯一能看出你的代码有问题的是你获取了访问令牌但实际上没有使用它Oauth2Authentication.php
function getOauth2Client() {
try {
$client = buildClient();
// Set the refresh token on the client.
if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
}
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
return $client;
} else {
// We do not have access request access.
header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
我在 Youtube 上 1 小时后遇到 0auth 问题已经被封锁了两天 api。 401 凭据错误。
$OAUTH2_CLIENT_ID = 'XXXXX';
$OAUTH2_CLIENT_SECRET = 'XXXXX';
$client = new Google_Client();
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$service = new Google_Service_YouTube($client);
if (isset($_GET['code'])){
$accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($accessToken);
$test=$client->getAccessToken();
//TEST REFRESH TOKEN
print_r($test);
sleep(10);
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$test=$client->getAccessToken();
print_r($test);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
...
foreach ($files as $file){
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
} // end if token expired
====CALL YOUTUBE API HERE IN FOREACH LOOP ===
}// end foreach files
}
我的测试代码结果显示访问令牌在提供刷新令牌后根本没有改变,甚至过期时间也没有减少'expires_in'
这就是为什么我在一个小时后遇到凭据错误...我不知道我的代码有什么问题,请帮助我。
这是我在获取访问代码后的测试代码的结果,所以你可以看到 'new' 访问令牌与前一个类似,我已经尝试在参数上也使用 encode_json
setAccessToken()
和 fetchAccessTokenWithRefreshToken()。没有收到错误,但结果仍然相同...
Array ( [access_token] => ya29.GlvQBuGBfZDQn3E8HWd4wfSbb0hLHsYVGzPBE0boJuB4ien5pcsOGqXlkEyOU7mevDLOGOWbuakTyTiAUVf2bkxNwZXX [expires_in] => 3600 [refresh_token] => 1/KEgjy2t9kTNwCXk-ZtMTSzPSS2xl4XX [scope] => https://www.googleapis.com/auth/youtube [token_type] => Bearer [created] => 1552891085 )
Array ( [access_token] => ya29.GlvQBuGBfZDQn3E8HWd4wfSbb0hLHsYVGzPBE0boJuB4ien5pcsOGqXlkEyOU7mevDLOGOWbuakTyTiAUVf2bkxNwZXX [expires_in] => 3600 [refresh_token] => 1/KEgjy2t9kTNwCXk-ZtMTSzPSS2xl4XX [scope] => https://www.googleapis.com/auth/youtube [token_type] => Bearer [created] => 1552891085 )
谢谢
访问令牌在一小时后过期,这就是它们的工作方式。访问令牌过期后,您应该 运行 您的代码并获取新的访问令牌。在新的访问令牌过期之前获取新的访问令牌将导致相同的访问令牌。
您的访问令牌是在创建时创建的(提示 epoch converter)添加 3600(秒)以了解它何时过期。
1552891085 <--- Monday, March 18, 2019 6:38:05 AM
我唯一能看出你的代码有问题的是你获取了访问令牌但实际上没有使用它Oauth2Authentication.php
function getOauth2Client() {
try {
$client = buildClient();
// Set the refresh token on the client.
if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
$client->refreshToken($_SESSION['refresh_token']);
}
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
return $client;
} else {
// We do not have access request access.
header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}