我米使用DRIVE V2與服務帳戶下載CSV文件,這一個是工作的罰款。我想遷移DRIVE V2到DRIVE V3。所以我改變了我的腳本按照谷歌文檔下載CSV文件會拋出錯誤403
I. Download a file in drive V3
PHP Library & Drive API V3此示例中使用如下:
1.Sample腳本使用Drive V3下載CSV文件
使用的方法:使用ALT =媒體
原因:這種方法只適用於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='[email protected]';
//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."
}
}
II。 Download a file in Drive V2
我用備用方法從驅動器下載CSV文件。
PHP Library & Drive API V2此樣品中使用:
使用驅動器V2 2.Sample腳本下載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([email protected]',"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);
}
結果:
PLZ幫我解決使用G盤V3下載一個CSV文件。是否有任何迴歸或功能滯後於V2/V3?
更新了發佈。 – Sattanathan
很難理解你的設置;我試着通過嘗試與雲端硬盤進行不同的溝通方式來排除問題,但是您需要閱讀您鏈接的7個不同的教程。您應該嘗試將其縮小,請閱讀http://stackoverflow.com/help/mcve。也許發佈失敗的線路和錯誤就足夠了。 –
感謝Pietro Saccardi,現在我改變了希望可以理解! – Sattanathan