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

时间:2013-12-05 10:38:36

标签: php access-token google-authentication

我正在尝试使用首次登录时提供的刷新令牌获取新的访问令牌。我正在使用以下代码来获取用户信息,但在访问令牌过期时无法获取该信息

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 个答案:

没有答案