【发布时间】:2014-03-07 04:05:29
【问题描述】:
使用 Google API for PHP 获取新的 Oauth2 访问令牌后,新提供的访问令牌报告已过期,没有可用的刷新令牌。下面,我通过 PHP 发送一个 CURL 调用来获取我的新访问令牌,它工作正常:
//Get new access token from Google
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,array(
'refresh_token' => $row['refresh_token'],
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'refresh_token',));
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
$json_data = curl_exec($curl);
这将成功返回新访问令牌的 JSON 表示。但是,当我尝试使用该 JSON 将一些基本文本添加到用户的 Glass 时间轴时,我收到以下错误:
An error occurred: The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.
我用来尝试插入的代码如下所示:
//Configure the Google Client
$client = new Google_Client();
$client -> setClientId($clientId);
$client -> setClientSecret($clientSecret);
$client -> setRedirectUri($redirectUri);
$client -> setAccessToken($json_data);
$client -> setScopes(array(
'https://www.googleapis.com/auth/glass.timeline',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/glass.location',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me'));
//Start a Mirror service
$mirror_svc = new Google_Service_Mirror($client);
//Filter the text to prevent ultimate destruction
$text = filter_var($_POST['t_text'],FILTER_SANITIZE_SPECIAL_CHARS);
//Attempt to add text to timeline
$result = (insertTimelineItem($mirror_svc,$text,null,null,null) ? 'success' : 'failure');
我觉得我错过了一些简单的东西,我只是不确定是什么。任何见解将不胜感激。
注意:post 变量只是临时变量,用于测试。
【问题讨论】:
标签: php oauth oauth-2.0 google-glass