Google oauth2.0 php客户端交换访问令牌带有刷新令牌



我正在尝试使用刷新令牌来交换访问令牌后,访问令牌过期。但是,我在这样做时一直面临着" Invalid_grant"之类的问题。

我想在尝试将refresh_token交换为新的access_token时存在一些问题。我的代码如下所示:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("My app");
$client->setApprovalPrompt (auto); //prompt consent screen only for first time
//*********** Replace with Your API Credentials **************
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://example.com/oauth2callback');
$client->setDeveloperKey('xxx');
//************************************************************
$client->setScopes(array('https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email'));
$client->refreshToken(file_get_contents('refreshtoken.conf')); //retrieve refresh_token from file
$client->setAccessToken("refresh_token"); // I guess something is wrong here. I am trying to pass the refresh token to get a new access token but not sure if this is correct
$client->authenticate(); //after that authenticate user
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client); // Call the OAuth2 class for get email address
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
  $user = $oauth2->userinfo->get();
  $me = $plus->people->get('me');
  $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
  $optParams = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);
  $jsonarray = json_decode($client->getAccessToken());
  $arrGoogleAuth['access_token']=$jsonarray->access_token;
  $arrGoogleAuth['refresh_token']=$jsonarray->refresh_token;
    //filewrite
  $myFile = "refreshtoken.conf";
  $fh = fopen($myFile, 'w') or die("can't open file"); //write the json into refresh.conf
  fwrite($fh, $client->getAccessToken());
  fclose($fh);
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>

而没有成为PHP客户端库的专家,查看代码,我相信您的代码的以下子集/稍微更改应该足够(假设您先前已成功请求离线访问,恢复刷新令牌并持续 $client->getAccessToken()的输出)(应该是JSON对象):

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("My app");
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://example.com/oauth2callback');
$client->setDeveloperKey('xxx');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email'));
$client->setAccessToken(file_get_contents('refreshtoken.conf'));
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client); 

基本上,$client->getAccessToken()的输出包括访问令牌,刷新令牌(如果被授予),时间到实时信息等。假设您坚持使用JSON BLOB,则可以设置它,您可以继续使用Google_plusservice(和其他Google(和其他Google)API) - 他们将自动检查访问令牌是否过期,并使用刷新令牌根据需要获取新的访问令牌。

if ($client->getAccessToken()) {
  if($client->isAccessTokenExpired()) {
     $client->authenticate();
     $NewAccessToken = json_decode($client->getAccessToken());
     $client->refreshToken($NewAccessToken->refresh_token);
    } else {
      $user = $oauth2->userinfo->get();
      $me = $plus->people->get('me');
      $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
      $optParams = array('maxResults' => 100);
      $activities = $plus->activities->listActivities('me', 'public', $optParams);
      $_SESSION['access_token'] = $client->getAccessToken();
    }
} else {
  $authUrl = $client->createAuthUrl();
}

相关内容

  • 没有找到相关文章

最新更新