带有刷新令牌的Google OAuth2.0 php客户端交换访问令牌
我试图在访问令牌过期后使用刷新令牌交换访问令牌。然而,在这样做的过程中,我一直面临着“无效补助金”这样的问题 我想在尝试将刷新令牌交换为新的访问令牌时出现了一些问题。我的代码如下所示:带有刷新令牌的Google OAuth2.0 php客户端交换访问令牌,php,google-api,google-oauth,access-token,google-api-php-client,Php,Google Api,Google Oauth,Access Token,Google Api Php Client,我试图在访问令牌过期后使用刷新令牌交换访问令牌。然而,在这样做的过程中,我一直面临着“无效补助金”这样的问题 我想在尝试将刷新令牌交换为新的访问令牌时出现了一些问题。我的代码如下所示: <?php require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_PlusService.php'; require_once 'g
<?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对象):
基本上,$client->getAccessToken()
的输出包括一个访问令牌、刷新令牌(如果被授予)、生存时间信息等。假设您保留该JSON blob,然后设置它,您可以继续使用Google_PlusService(和其他Google API)-他们将自动检查访问令牌是否过期,并根据需要使用刷新令牌获取新的访问令牌。嗨,aeijdenberg。根据您的建议,刷新令牌将用于从Google server交换访问令牌。我现在有另一个问题,每个登录到我的系统的用户都将使用我的帐户登录因为我有点喜欢将我的刷新令牌硬编码到程序中。你有什么建议可以让每个用户都有自己的刷新令牌吗?我认为我把它保存在refreshtoken.conf中的想法是不正确的。正确-你不应该将它存储在refreshtoken.conf中-我假设这是用于测试的。Y您需要将其保存在任何适合您的应用程序的数据库/存储系统中。如果您不需要实际进行脱机访问(当用户不在场时进行访问)您可以请求一个访问令牌,然后将其存储在用户会话中,这可能会更简单。我不是PHP专家,因此无法为您提供最佳指导。我实际上不需要脱机访问。我尝试将访问令牌存储在会话中。不知怎的,访问令牌将在一小时后过期,那么用户将无法访问要访问已经存在的内容,他们需要先清除浏览缓存才能再次登录。有什么建议吗?再次感谢,伙计。
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();
}
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();
}