使用 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 文档

更改了我的脚本

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."
 }
}

二. Download a file in Drive V2

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

本例中使用的

PHP Library & Drive API V2

2.Sample 使用 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 文件记录,工作正常

请帮我解决使用G DRIVE V3下载CSV文件的问题。是否有任何回归或函数滞后 b/w V2、V3?

由于 PHP 的 Google Drive API 是测试版,请注意开发人员可能会遇到一些错误。

Error calling GET https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media: (302) Moved Temporarily The document has moved here.

在这种情况下,这里的link是: https://www.googleapis.com/下载/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=媒体

您可以看到 API 服务器在“/drive...”之前建议新的 link "download"。

在Google Drive Client Library V3 中,这是您可以手动修复的解决方案,方法是将以下代码添加到src/Google/Http/REST。php 第147 行之后:

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

希望这对您有所帮助...:)