2016-03-08 104 views
0

我米使用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); 
} 

结果:

我得到了CSV文件中的记录,做工精细 enter image description here

PLZ帮我解决使用G盘V3下载一个CSV文件。是否有任何回归或功能滞后于V2/V3?

+2

更新了发布。 – Sattanathan

+0

很难理解你的设置;我试着通过尝试与云端硬盘进行不同的沟通方式来排除问题,但是您需要阅读您链接的7个不同的教程。您应该尝试将其缩小,请阅读http://stackoverflow.com/help/mcve。也许发布失败的线路和错误就足够了。 –

+0

感谢Pietro Saccardi,现在我改变了希望可以理解! – Sattanathan

回答

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服务器提出了新的链接“下载“之前”/驱动器...“。

在谷歌驱动器客户端库V3,这里是你可以通过线147后添加以下代码为src /谷歌/ HTTP/REST.php手动修复解决方案:

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

希望这有助于.. 。:)

+0

谢谢Sotheayuth。它工作正常! – Sattanathan