Google API 刷新令牌
Google API Refresh Token
在过去的几个小时里,我一直在尝试从最初的 Google API 调用中打印我的 refresh_token (即使在取消授权和重新授权之后,正如其他人所建议的那样做)但我仍然没有任何运气...... API 按预期工作但我想设置刷新功能以便我的用户在令牌过期后不会失去访问权限(导致错误)。
目前,以下代码将 $_SESSION['refresh_token'] 设置为空。
任何帮助将不胜感激,因为我已经用头敲击键盘很长一段时间了...
private function googleAnalyticsClient(){
App::import('Vendor', 'google-api-php-client', array('file' => 'autoload.php'));
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_Analytics($client);
return $analytics;
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
public function getOauth()
{
App::import('Vendor', 'google-api-php-client', array('file' => 'autoload.php'));
// Create the client object and set the authorization configuration
// from the client_secrets.json you downloaded from the Developers Console.
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$_SESSION['refresh_token'] = $client->getRefreshToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/dashboard/tpstats';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
在 getOauth() 函数中添加以下内容
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$client->setAccessType('offline'); //To fetch refresh token(Refresh token issued only first time)
$client->setApprovalPrompt('force'); //Adding this will force for refresh token
在过去的几个小时里,我一直在尝试从最初的 Google API 调用中打印我的 refresh_token (即使在取消授权和重新授权之后,正如其他人所建议的那样做)但我仍然没有任何运气...... API 按预期工作但我想设置刷新功能以便我的用户在令牌过期后不会失去访问权限(导致错误)。
目前,以下代码将 $_SESSION['refresh_token'] 设置为空。
任何帮助将不胜感激,因为我已经用头敲击键盘很长一段时间了...
private function googleAnalyticsClient(){
App::import('Vendor', 'google-api-php-client', array('file' => 'autoload.php'));
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_Analytics($client);
return $analytics;
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
public function getOauth()
{
App::import('Vendor', 'google-api-php-client', array('file' => 'autoload.php'));
// Create the client object and set the authorization configuration
// from the client_secrets.json you downloaded from the Developers Console.
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$_SESSION['refresh_token'] = $client->getRefreshToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/dashboard/tpstats';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
在 getOauth() 函数中添加以下内容
$client = new Google_Client();
$client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$client->setAccessType('offline'); //To fetch refresh token(Refresh token issued only first time)
$client->setApprovalPrompt('force'); //Adding this will force for refresh token