【发布时间】:2018-09-17 18:22:17
【问题描述】:
我正在处理一个分析 API 项目,当访问令牌在一小时后过期时,我得到“代码:401,无效凭据”并且必须取消设置 php 会话才能重新进行身份验证。我已经读到我应该在首次授权时自动收到一个刷新令牌,但我在任何地方都没有看到它。我还尝试手动设置刷新令牌,从 oauth2 游乐场获得它。我怎样才能使这项工作?刷新令牌最初是否应该存储在 $_SESSION 中?
有问题的代码:
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setIncludeGrantedScopes(true);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_Analytics($client);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
$_SESSION 中的访问令牌
[access_token] => blahblahblah
[expires_in] => 3600
[scope] => https://www.googleapis.com/auth/analytics.readonly
[token_type] => Bearer
[created] => 1537207944
【问题讨论】:
标签: php oauth-2.0 google-oauth google-analytics-api