如何从php中的刷新令牌获取新的访问令牌

时间:2015-03-30 11:24:13

标签: php google-api-php-client

我正在使用谷歌API来获取访问令牌和刷新令牌。但我的问题是如何使用此刷新令牌获取新的访问令牌,然后我可以再次更新用户详细信息。我拥有所有凭据,并且我获得了存储到数据库中的第一个访问令牌和刷新令牌。我想获得新的访问令牌,我必须再次存储到数据库中。我怎样才能做到这一点。 我试着这个......我不知道它是否正确..

 if (isset($_GET['code'])) {
 $client->setAccessType('offline');

$client->authenticate($_GET['code']);
$gClient->setAccessType('offline');
$_SESSION['token'] = $client->getAccessToken();


$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
return;
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}

if($client->isAccessTokenExpired()) 
{
$client->setAccessType('offline');

   if (isset($_GET['code'])) 
   {
   $client->authenticate();
   $_SESSION['access_token'] = $client->getAccessToken();
   }

  if (isset($_REQUEST['logout']))
  {
  unset($_SESSION['token']);
  $client->revokeToken();
  }
  ?>
   <!doctype html>
   <html>
  <head><meta charset="utf-8"></head>
   <body>
   <header><h1>Get Token</h1></header>
   <?php
   if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
    $token = json_decode($_SESSION['token']);

    echo "Access Token = " . $token->access_token . '<br/>';
    echo "Refresh Token = " . $token->refresh_token . '<br/>';
    echo "Token type = " . $token->token_type . '<br/>';
    echo "Expires in = " . $token->expires_in . '<br/>';
    echo "ID Token = " . $token->id_token . '<br/>';
    echo "Created = " . $token->created . '<br/>';
    echo "<a class='logout' href='?logout'>Logout</a>";
    } else {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
  }
  ?>
 </body>
 </html>

我会写一个文件来刷新访问令牌,或者我可以在一个文件上做。

2 个答案:

答案 0 :(得分:1)

第一次获得令牌后:

$code = FIRST_TIME_TOKEN;

$client = new Google_Client();
                $client->setClientId(self::OAUTH2_CLIENT_ID);
                $client->setClientSecret(self::OAUTH2_CLIENT_SECRET);
                $client->setScopes(SCOPE_URL);
                $client->setAccessType('offline');
                $redirect = filter_var(REDIRECT_URL,
                  FILTER_SANITIZE_URL);
                $client->setRedirectUri($redirect);

                $youtube = new Google_Service_YouTube($client); // You can change it

                if (isset($code)) {

                      if (strval($_SESSION['state']) !== strval($_GET['state'])) {
                        throw new \Exception('Session expired , Please refresh the page');
                      }

                      $client->authenticate($code);
                      $_SESSION['token'] = $client->getAccessToken();
                      header('Location: ' . $redirect);
                }

                $completeYoutubeResponse=json_decode($_SESSION['token']); 
                if(!isset($completeYoutubeResponse->access_token) || !isset($completeYoutubeResponse->refresh_token)) { 

                    throw new \Exception('This youtube account is used by some other user');
                } else {

$accessToken=$completeYoutubeResponse->access_token;   // We need to save it in db

$refreshToken=$completeYoutubeResponse->refresh_token;   // We need to save it in db

                     return (isset($completeYoutubeResponse) ? $completeYoutubeResponse : null);

答案 1 :(得分:0)

使用:

$token = json_decode($_SESSION['token']);
$client->refreshToken($token->refresh_token);
$_SESSION['token'] = $client->getAccessToken();