如何知道Google Api PHP中的访问令牌是否过期 [英] How to know if Access Token expires in Google Api PHP
本文介绍了如何知道Google Api PHP中的访问令牌是否过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何知道我的访问令牌是否已过期。
How will I know if my access token has expired.
我正在使用try和catch
I'm using try and catch
try {
$result = file_get_contents('https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token='.$accesstoken);
print_r($result);
}
catch(Exception $e){
echo "Get new token";
}
但仍然从file_get_contents获取错误,然后打印获取新令牌
but still it gets error from the file_get_contents then prints "Get new token"
如果我想使用curl
If I want to use curl
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token='.$accesstoken);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
那么我该怎么做来捕获错误?
then what should I do to catch the error here?
//if error because access token is invalid,
do here
// my fixed solution
$response= json_decode($result);
if($response->error){ // if result has errors
echo "Get new token";
}
推荐答案
p>
probing this url:
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={accessToken}
会为您提供:
{
"audience":<your_client_id>,
"user_id":<user_id_if_userinfo.profile_was_authorized>,
"scope":"<authorized_scope_1> <authorized_scope_1>",
"expires_in":<time_to_live>
}
或错误:
{"error":"invalid_token"}
这篇关于如何知道Google Api PHP中的访问令牌是否过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文