使用Google Drive V3下载CSV文件(V2完美无缺)会抛出错误403

时间:2016-03-08 10:13:12

标签: php google-app-engine csv google-drive-api

我使用 DRIVE V2 WITH 服务帐户下载CSV文件,这个工作正常。我想将DRIVE V2迁移到DRIVE V3。所以我按照下面的谷歌文档更改了我的脚本

I. Download a file in drive V3

PHP Library&此示例中使用了Drive API V3

1.Sample脚本使用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."
 }
}

II。 Download a file in Drive V2

我使用替代方法从驱动器下载CSV文件。

PHP Library&amp;此示例中使用了Drive 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);
}

结果:

我收到了CSV文件记录,工作正常 enter image description here

Plz帮我解决使用G DRIVE V3下载CSV文件的问题。是否有任何回归或功能滞后于V2 / V3?

1 个答案:

答案 0 :(得分:1)

由于适用于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服务器建议新链接&#34; download&#34;之前&#34; /驱动...&#34;。

在Google Drive Client Library V3中,您可以通过在第147行之后将以下代码添加到src / Google / Http / REST.php来手动修复此解决方案:

if($requestUrl=='drive/v3/files/{fileId}' && $params['alt']['value']=='media')
  $requestUrl = "download/".$requestUrl;

希望这有帮助...:)