Php Google API查询刷新令牌

Php Google API查询刷新令牌,php,google-api,Php,Google Api,我已经编写了一个脚本,它使用PHPSDK成功地向Google查询访问令牌 我的代码 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

我已经编写了一个脚本,它使用PHPSDK成功地向Google查询访问令牌

我的代码

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));
}
require_once(“uu/libs/google/apiclient/autoload.php”);
$client_id='';
$client_secret='';
$redirect_uri='';
//配置我的应用程序信息
$client=新的Google_客户端();
$client->setClientId($client\u id);
$client->setClientSecret($client\u secret);
$client->setRedirectUri($redirect\u uri);
$client->addScope(“https://mail.google.com/");
//当google用代码交换的方式重定向访问令牌时
如果(isset($_GET['code'])){
$client->authenticate($_GET['code']);
$\会话['access\u token']=$client->getAccessToken();
$\会话['refresh\u token']=$client->getRefreshToken();
$redirect='http://'.$\u服务器['http\u主机].$\u服务器['PHP\u SELF'];
标题('Location:'.filter_var($redirect,filter_SANITIZE_URL));
}
一旦我获得如上所示的身份验证代码,并通过
getAccessToken()查询访问令牌返回创建和到期时间

但是,当我通过
getRefreshToken()查询刷新令牌时将其输出到屏幕时不返回任何内容

有人问了一个类似的问题


问题似乎是让谷歌知道你需要离线访问,但这个问题的答案不能转移到PHP SDK。

虽然我使用的是JavaScript,但我也遇到了同样的问题。我通过将redirect_uri参数添加到初始令牌请求调用中解决了这个问题

以下是我的SO帖子和我的解决方案:

为了澄清流程,要获取刷新令牌,您实际上必须请求从Google获取“代码”(因此我的参数
'response\u type':“code”
),然后您使用该代码再次调用以获取访问令牌和刷新令牌