具有离线访问功能的Google Drive api

时间:2014-07-13 15:52:52

标签: php google-api-php-client

我正在使用以下代码将文件上传到Google云端硬盘,通过刷新令牌使用离线访问API:

<?php
include 'config.php';
$client_id      = XXXX;
$client_secret  = XXXX;
$redirect_uri   = XXXX;
$refreshToken   = XXXX; //stored refresh token during the first time authendication

function get_oauth2_token($grantCode) 
{
global $config;

$client_id      = XXXX;
$client_secret  = XXXX;
$redirect_uri   = XXXX;

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"client_id" => $client_id,
"client_secret" => $client_secret);

$clienttoken_post["refresh_token"] = $grantCode;
$clienttoken_post["grant_type"] = "refresh_token";

$curl = curl_init($oauth2token_url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($curl);
curl_close($curl);
return $json_response;

}   
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$drive = new Google_Client();

$drive->setClientId($client_id); // HERE I WRITE MY Client ID
$drive->setClientSecret($client_secret); // HERE I WRITE MY Client Secret


$gdrive = new Google_Service_Drive($drive);
$response = get_oauth2_token($refreshToken);//i am getting the access token in json format here
$drive->setAccessToken($response);

$doc = new Google_Service_Drive_DriveFile();
$doc->setTitle('Test Drive');
$doc->setDescription('Document');
$doc->setMimeType('text/plain');

$content = file_get_contents('files/example.txt');

$output = $gdrive->files->insert($doc, array(
  'data' => $content,
  'mimeType' => 'text/plain',
));

print_r($output);

?>

当我尝试执行上述脚本时,我收到此错误:

  

致命错误:未捕获的异常“Google_Auth_Exception”,并显示消息“OAuth 2.0访问令牌已过期,并且刷新令牌不可用。对于自动批准的回复,不会返回刷新令牌。在D:\ karthi \ xampp-win32-1.8.2-1-VC9 \ xampp \ htdocs \ apis \ google_drive \ Google \ Auth \ OAuth2.php:222堆栈跟踪:#0 D:\ karthi \ xampp-win32-1.8 .2-1-VC9 \ xampp \ htdocs \ apis \ google_drive \ Google \ Service \ Resource.php(176):Google_Auth_OAuth2-&gt; sign(Object(Google_Http_Request))#1 D:\ karthi \ xampp-win32-1.8。 2-1-VC9 \ xampp \ htdocs \ apis \ google_drive \ Google \ Service \ Drive.php(1754):Google_Service_Resource-&gt; call('insert',Array,'Google_Service _...')#2 D:\ karthi \ xampp-win32-1.8.2-1-VC9 \ xampp \ htdocs \ apis \ google_drive \ offline.php(65):Google_Service_Drive_Files_Resource-&gt; insert(Object(Google_Service_Drive_DriveFile),Array)#3 {main}抛出D:第222行的\ karthi \ xampp- win32-1.8.2-1-VC9 \ xampp \ htdocs \ apis \ google_drive \ Google \ Auth \ OAuth2.php

我正在使用这个php客户端库GOOGLE PHP CLIENT

请帮帮我。

0 个答案:

没有答案