2014-11-24 125 views
0

我正在尝试创建一个Web应用程序,该应用程序在我的Google云端硬盘帐户中显示并提供文件链接列表。我不希望每次都要授权登录,所以我使用离线模式每次都会通过刷新令牌的生成。OAuth访问Google Drive授权问题

使用oAuth Playground我生成了我的刷新令牌,并试图返回文件属性的完整列表,但我收到错误消息'出现错误:OAuth 2.0访问令牌已过期,并且刷新令牌为无法使用。刷新令牌不会返回自动批准的响应。()'。即使我在提交我的请求之前直接生成访问令牌。

我很确定我正在做第一个位,因为我的curl请求正在返回包含访问令牌的令牌详细信息。第二点我做错了吗?

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"https://accounts.google.com/o/oauth2/token"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,"refresh_token=MyRefreshTokenFromOAuthPlayground&client_id=MyClientID&client_secret=MyClientSecret&grant_type=refresh_token"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$server_output = curl_exec ($ch); 
curl_close ($ch); 

$myjson = json_decode($server_output,true);   
//at this point if I show $myjson['access_token'] I am given what looks like a valid access token. 

$client = new Google_Client(); 
$client->setClientId('MyClientID'); 
$client->setClientSecret('MyClientSecret'); 
$client->setRedirectUri('https://developers.google.com/oauthplayground'); 
$client->setScopes(array(
    'https://www.googleapis.com/auth/drive')); 
$client->setAccessType("offline"); 
$client->setAccessToken($server_output); //the results of the curl request 
$client->setUseObjects(true); 
$service = new Google_DriveService($client); 


echo print_r(retrieveAllFiles($service)); //this is the sample function in the API documentation that gives the full list of file properties. It is this command that causes the error. 

回答

0

的谷歌API的PHP客户端库添加额外的字段created令牌对象时,它执行的授权流程,它采用了createdexpires_in领域determine if the token is still valid。如果缺少created字段,则认为该令牌无效。您可以自己设置此字段,但更好的解决方案是仅使用库的现有功能来为您执行所有令牌提取。

$client->refreshToken('MyRefreshTokenFromOAuthPlayground');