我正在编写一些脚本,以便在google drive中下载我的文件。
here my test.php
require_once 'google-api-php-client/Google_Client.php';
require_once 'google-api-php-client/contrib/Google_DriveService.php';
$drive = new Google_Client();
$drive->setClientId('709390325823-6bljk00f39xxxxx6jg3ji.apps.googleusercontent.com');
$drive->setClientSecret('UL07mxxxxx01eA');
$drive->setRedirectUri('http://xxx');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$drive->setAccessType('offline');
$gdrive = new Google_DriveService($drive);
$url = $drive->createAuthUrl();
$token = $drive->authenticate($authorizationCode);
$fileId = "0B9LPSEw89uQNQVNuUzdwM2hFMlk";
$file = $gdrive->files->get($fileId);
$downloadUrl = $file->downloadUrl;
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$io = $drive->getIo();
$httpRequest = $io->makeRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
error_log("Error Downloading Attachment - HTTP ResponseCode:".$httpRequest->getResponseHttpCode());
error_log("ContentUrl: ".$attachment->getContentUrl());
error_log("ContentUrl: ".$attachment->getContentUrl());
error_log("ContentType: ".$attachment->getContentType());
error_log("id ".$attachment->getId());
error_log("IsProcessing: ".(int)$attachment->getIsProcessingContent());
return null;
}
} else {
// An error occurred.
}
一切看起来都很好,当我运行没有下载url,它显示下载url和令牌
https://doc-0g-1s-docs.googleusercontent.com/docs/securesc/qjrgdcogjm0ajgdp4fnjceh8pfg486sg/u39dld0fisj9v0b3ih1nmchqp4495gdl/1473127200000/02390580194345273532/02390580194345273532/0B9LPSEw89uQNQVNuUzdwM2hFMlk?e=download& gd = true& access_token = ya29.Ci9WAxf99iwravP2CMmXEaYJWZb-1Y2Dxze1HToAyLb0_JJGrdVjFA5goQVBVRmWKg
但是显示错误
致命错误:在
中的非对象上调用成员函数getResponseHttpCode()
在API Google v2中HTTP响应是什么功能?我调用函数是错的还是缺少了什么?
希望你们给点建议。由于
我建议检查一下PHP客户端库实现的样例代码。
下面是下载Google文档(文档,电子表格,演示文稿等)并将其导出为应用程序可以处理的格式的文档示例代码。
Google Drive v2:
/**
* Download a file's content.
*
* @param Google_Servie_Drive $service Drive API service instance.
* @param Google_Servie_Drive_DriveFile $file Drive File instance.
* @return String The file's content if successful, null otherwise.
*/
function downloadFile($service, $file) {
$downloadUrl = $file->getDownloadUrl();
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$httpRequest = $service->getClient()>getAuth()>authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
下面是文档中的示例代码。
Google Drive v3。
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
'alt' => 'media' ));
希望有帮助!