1
我写了一个脚本,成功地向谷歌查询使用PHP SDK的访问令牌。谷歌API刷新令牌
我的代码,
require_once('_/libs/google/apiclient/autoload.php');
$client_id = '<MY_ID>';
$client_secret = '<MY_SECRET>';
$redirect_uri = '<REDIRECT_URL>';
// Config my apps information
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://mail.google.com/");
// When google redirects with code swap for access token
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
<------------- Try to get both tokens ----------------->
$_SESSION['access_token'] = $client->getAccessToken();
$_SESSION['refresh_token'] = $client->getRefreshToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
一旦我得到了验证码如上和查询显示了访问令牌通过getAccessToken();
它的创建和到期时间返回。
但是,当我通过getRefreshToken();
查询刷新令牌时,它在输出到屏幕时不会返回任何内容。
也有类似的问题问here
的问题似乎让谷歌知道你需要离线访问然而回答这个问题是不可转让的PHP SDK。