[英]Download CSV file using Google Drive V3(V2 works perfect) throws error 403
我正在使用帶有服務帳戶的DRIVE V2下載 CSV 文件,這個工作正常。 我想將 DRIVE V2 遷移到 DRIVE V3。所以我按照下面的谷歌文檔更改了我的腳本
本示例中使用的PHP 庫和驅動器 API V3 :
1.使用Drive V3下載CSV文件的示例腳本
使用的方法:使用 alt=media
原因:此方法僅在 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);
}
結果:我遇到了以下問題
Error calling GET https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media: (302)
Moved Temporarily
The document has moved here.
點擊這里后。 我看到下面的錯誤。
{
"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."
}
}
我使用替代方法從驅動器下載 CSV 文件。
本示例中使用的PHP 庫和驅動器 API V2 :
2.Sample Script 使用 Drive V2 下載 CSV 文件
使用的方法:替代方法:使用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);
}
結果:
請幫助我解決使用 G DRIVE V3 下載 CSV 文件的問題。 是否有任何回歸或函數滯后於黑白 V2、V3?
由於適用於 PHP 的 Google Drive API 是測試版,因此開發人員可能會遇到一些錯誤。
調用 GET https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media 時出錯:(302) 臨時移動文檔已移至此處。
在這種情況下,這里的鏈接是: https://www.googleapis.com/ ?下載/驅動器/ V3 /文件/ 0B5pkfK_IBDxjeHlTTDFFY01CXzQ ALT =媒體
您可以看到 API Server 在“/drive...”之前建議新鏈接“下載”。
在 Google Drive Client Library V3 中,這里是您可以通過將以下代碼添加到 src/Google/Http/REST.php 的第 147 行之后手動修復的解決方案:
if($requestUrl=='drive/v3/files/{fileId}' && $params['alt']['value']=='media')
$requestUrl = "download/".$requestUrl;
希望這有助於... :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.