OAuth访问Google云端硬盘授权问题

时间:2014-11-24 21:34:56

标签: php curl oauth google-drive-api google-oauth

我正在尝试创建一个在我的Google云端硬盘帐户中查找并提供文件链接列表的网络应用。我不想每次都要授权登录,所以我每次都使用离线模式来购买刷新令牌。

使用oAuth Playground我已经生成了我的刷新令牌,并且我正在尝试返回文件属性的完整列表,但是我收到错误消息“发生错误:OAuth 2.0访问令牌已过期,并且刷新令牌不是可用。对于自动批准的响应,不会返回刷新令牌.Array()'。即使我在提交请求之前直接生成访问令牌。

我很确定我正在做第一位正确,因为我的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.

1 个答案:

答案 0 :(得分:0)

Google API PHP客户端库在执行授权流程时将额外字段created添加到令牌对象,并使用createdexpires_in字段{{3} }。如果缺少created字段,则假定令牌无效。您可以自己设置此字段,但更好的解决方案是使用库的现有功能为您执行所有令牌提取。

$client->refreshToken('MyRefreshTokenFromOAuthPlayground');
相关问题