使用Google云端硬盘V3(V2工作正常)下载CSV文件会抛出错误403 [英] Download CSV file using Google Drive V3(V2 works perfect) throws error 403
问题描述
我使用 DRIVE V2 与服务帐户下载CSV文件,这一个工作正常。我要将DRIVE V2迁移到DRIVE V3。我已根据以下Google文档更改了我的脚本
Plz帮我解析使用G DRIVE V3下载CSV文件。有没有任何回归或函数滞后b / w V2,V3?
由于Google Drive API for PHP是测试版,
错误调用GET
https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media :
(302)暂时移动该文档已移动此处。
此处的链接为:
https://www.googleapis.com/ 下载 / drive / v3 / files / 0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt = media
您可以看到API服务器建议新的链接 / drive ...。
在Google云端硬盘客户端库V3中,您可以通过将以下代码添加到src / Google / Http / REST .php第147行后:
if($ requestUrl =='drive / v3 / files / {fileId}'&& ; $ params ['alt'] ['value'] =='media')
$ requestUrl =download /\".$ requestUrl;
希望有帮助...:)
I m using DRIVE V2 WITH Service Account to download CSV file,This one is working fine. I want to migrate DRIVE V2 to DRIVE V3 .So i changed my script as per below google documents
I. Download a file in drive V3
PHP Library & Drive API V3 used in this sample:
1.Sample script download CSV file using Drive V3
Method used: Using alt=media
Reason: this method only available in DRIVE V3
<?php
set_include_path( get_include_path() . PATH_SEPARATOR . 'Google' );
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
try{
//Get service document
$service = get_service_document();
//Download a csv file
$data = $service->files->get("FILE ID", array( 'alt' => 'media'));
print_r($data);
}
catch(Exception $e){
print_r($e->getMessage());
}
//function to get service
function get_service_document(){
$userstamp='user@example.com';
//Enable below two lines if let know the clientid,tokens,etc.,
$driveService=buildServiceDrive($userstamp,"SERVICE_ACCOUNT","https://www.googleapis.com/auth/drive","KEY.p12");
return $driveService;
}
//building service
function buildServiceDrive($userEmail,$service_id,$scope,$service_filename) {
$key = file_get_contents($service_filename);
$auth = new Google_Auth_AssertionCredentials(
$service_id,
array($scope),
$key);
$auth->sub = $userEmail;
$client = new Google_Client();
$client->setAssertionCredentials($auth);
return new Google_Service_Drive($client);
}
RESULT: I got the below issue
Error calling GET https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media: (302)
Moved Temporarily
The document has moved here.
After clicked here. I saw below error.
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
II. Download a file in Drive V2
I used alternate method to download CSV file from drive.
PHP Library & Drive API V2 used in this sample:
2.Sample Script download CSV file using Drive V2
Method used : Alternate method: using downloadUrl
<?php
set_include_path( get_include_path() . PATH_SEPARATOR . 'Google' );
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
try{
//Get service document
$service = get_service_document();
$data = $service->files->get("FILE ID");
$url=$data->downloadUrl;
$data=downloadFile($service,$url);
print_r($data);
}
catch(Exception $e){
print_r($e->getMessage());
}
//Alternate method using download URL
function downloadFile($service, $downloadUrl)
{
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 {
echo "errr";
return null;
}
} else {
echo "empty";
return null;
}
}
//function to get service
function get_service_document(){
$driveService =buildServiceDrive(user@example.com',"SERVICE-ACCOUNT","https://www.googleapis.com/auth/drive","KEY.p12");
return $driveService;
}
//building service
function buildServiceDrive($userEmail,$service_id,$scope,$service_filename) {
$key = file_get_contents($service_filename);
$auth = new Google_Auth_AssertionCredentials(
$service_id,
array($scope),
$key);
$auth->sub = $userEmail;
$client = new Google_Client();
$client->setAssertionCredentials($auth);
return new Google_Service_Drive($client);
}
Result:
i got CSV file records, working fine
Plz help me resolve to download a CSV file using G DRIVE V3. Is there any regression or functions lagging b/w V2, V3?
Since the Google Drive API for PHP is beta version, realize that some errors can be experienced by developers.
Error calling GET https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media: (302) Moved Temporarily The document has moved here.
In this case, here's link is: https://www.googleapis.com/download/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media
You can see the API Server suggest the new link "download" before "/drive...".
In Google Drive Client Library V3, here is the solution you can fix manually by adding the following code to src/Google/Http/REST.php after line 147:
if($requestUrl=='drive/v3/files/{fileId}' && $params['alt']['value']=='media')
$requestUrl = "download/".$requestUrl;
Hope this help... :)
这篇关于使用Google云端硬盘V3(V2工作正常)下载CSV文件会抛出错误403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!