I am trying to get a refresh token for the Google API's, using the PHP SDK. I am authenticating the user with Javascript, retrieving a code, and exchanging it for an access_token server side, but this doesn't grant me an access token. What am I doing wrong? Here is the code I use:
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->addScope('https://www.googleapis.com/auth/plus.me');
$client->addScope('https://www.google.com/m8/feeds');
$client->setRedirectUri('postmessage');
$client->setAccessType('offline');
if (isset($_REQUEST['code'])) {
$client->authenticate($_REQUEST['code']);
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
$token_data = $client->verifyIdToken()->getAttributes();
$result['data']=$token_data;
$result['access_token']=json_decode($_SESSION['access_token']);
}
}
debug($result); //my own function, var_dumps the content of an array
Here is the result of the array:
$result['access_token'] contains:
access_token: TOKEN
created: 1434380576
expires_in: 3594
id_token: IDTOKEN
token_type:"Bearer"
If I am not mistaken the first access token should also contain the refresh token, what am I doing wrong?
答案 0 :(得分:0)
首先检查Google开发人员控制台中的设置,看看您的RedirectUri是否相同以及API是否已激活(尽管如果您已经拥有.json,那么我认为它是。
您必须至少一次通过Google身份验证提示屏幕才能获得.json中的刷新令牌,如果您的RedirectUri无法带您,您将无法获得刷新令牌或甚至访问验证。
如果您正在进行小型文件交易,并且不需要对脚本进程进行用户验证,您也可以尝试使用服务帐户。祝你好运。
答案 1 :(得分:0)
问题在于我必须在身份验证过程中指定我要离线访问,客户端... Google API的记录非常糟糕!