dongyou8368 2013-12-05 10:38
浏览 38

如何在Google身份验证中使用刷新令牌获取访问令牌?

I'm trying to get new access token using the refresh token provided during the first login. I'm using the following code to get the user info ,but not able to get it when access token expires

function getTokenInfo($access_token,$refresh_token){
     require_once "google_oauth/src/Google_Client.php";
 try{

 $_googleClient = new Google_Client();
 $_googleClient->setClientId(CONSTANT::ClientID);
 $_googleClient->setClientSecret(CONSTANT::ClientSecret);
 $_googleClient->setRedirectUri(CONSTANT::RedirectURL);
 $_googleClient->setDeveloperKey(CONSTANT::DevelopersKey);
 $_googleClient->setScopes( "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile");

 $_tokenArray['access_token'] = $access_token;
 $_googleClient->setAccessToken(json_encode($_tokenArray));

 $refTokenArray['refresh_token'] = $refresh_token;
 $_googleClient->refreshToken(json_encode($refTokenArray));


 if($_googleClient->isAccessTokenExpired()) {

     $NewAccessToken = json_decode($_googleClient->getAccessToken());
     $access_token =$NewAccessToken->access_token;
 }

 }catch(Exception $e){

     echo $e->getMessage();
 }

     $info= json_decode(file_get_contents('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$access_token));

     return $info;
 }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离