I m using DRIVE V2 WITH Service Account to download CSV file,This one is working fine. I want to migrate DRIVE V2 to DRIVE V3 .So i changed my script as per below google documents
I. Download a file in drive V3
PHP Library & Drive API V3 used in this sample:
1.Sample script download CSV file using Drive V3
Method used: Using alt=media
Reason: this method only available in 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);
}
RESULT: I got the below issue
Error calling GET https://www.googleapis.com/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media: (302)
Moved Temporarily
The document has moved here.
After clicked here. I saw below error.
{
"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
I used alternate method to download CSV file from drive.
PHP Library & Drive API V2 used in this sample:
2.Sample Script download CSV file using Drive V2
Method used : Alternate method: using 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);
}
Result:
i got CSV file records, working fine
Plz help me resolve to download a CSV file using G DRIVE V3. Is there any regression or functions lagging b/w V2, V3?
Since the Google Drive API for PHP is beta version, realize that some errors can be experienced by developers.
In this case, here's link is: https://www.googleapis.com/download/drive/v3/files/0B5pkfK_IBDxjeHlTTDFFY01CXzQ?alt=media
You can see the API Server suggest the new link "download" before "/drive...".
In Google Drive Client Library V3, here is the solution you can fix manually by adding the following code to src/Google/Http/REST.php after line 147:
Hope this help... :)