Skip to content

Instantly share code, notes, and snippets.

@printminion
Last active September 8, 2022 00:42
Show Gist options
  • Save printminion/6624985accb5570aab2d to your computer and use it in GitHub Desktop.
Save printminion/6624985accb5570aab2d to your computer and use it in GitHub Desktop.
Download google drive spreadheet as csv
{
"name": "printminion/googlie-api-example",
"description": "csv drive download exaple",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Mischa M.-Kupriyanov",
"email": "m.kupriyanov"
}
],
"require": {
"components/google-api-php-client": "1.1.2"
},
"repositories": [
{
"type": "package",
"package": {
"name": "components/google-api-php-client",
"type": "component",
"version": "1.1.2",
"dist": {
"url": "https://github.com/google/google-api-php-client/archive/1.1.2.zip",
"type": "zip"
},
"autoload": {
"classmap": ["src/"]
}
}
}
]
}
<?php
session_start();
define('SPREADSHEET_ID', '<YOUR_SPREADSHEET_ID_HERE>');
define('SPREADSHEET_SHEET_ID', '<YOUR_SPREADSHEET_SHEET_ID_HERE>');
$configurationFile = getcwd() . '/client_secret.json';
if (!file_exists($configurationFile)) {
echo 'please client_secret.json is not available. Please download it form https://console.developers.google.com/' . PHP_EOL;
exit(1);
}
$CLIENT_SECRET = json_decode(file_get_contents($configurationFile), true);
require_once realpath(dirname(__FILE__) . '/../vendor/autoload.php');
$client_id = $CLIENT_SECRET['installed']['client_id'];
$client_secret = $CLIENT_SECRET['installed']['client_secret'];
$redirect_uri = $CLIENT_SECRET['installed']['redirect_uris'][0];
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive.readonly");
$service = new Google_Service_Drive($client);
$authUrl = $client->createAuthUrl();
define('ACCESS_TOKEN_FILE', dirname(__FILE__) . '/access_token.txt');
$accessToken = null;
if (file_exists(ACCESS_TOKEN_FILE)) {
$accessToken = file_get_contents(ACCESS_TOKEN_FILE);
}
if (empty($accessToken)) {
//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
$_GET['code'] = $authCode;
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
file_put_contents(ACCESS_TOKEN_FILE, $accessToken);
}
$client->setAccessToken($accessToken);
$file = $service->files->get(SPREADSHEET_ID, array());
$fileContents = downloadFile($service, $file, SPREADSHEET_SHEET_ID);
echo $fileContents;
function downloadFile($service, $file, $sheetId = 0)
{
$downloadUrls = $file->getExportLinks();
$downloadUrl = $downloadUrls['text/csv'] . '&gid=' . $sheetId;
if (empty($downloadUrl)) {
return null;
}
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
}
// An error occurred.
return null;
}
@kylanhurt
Copy link

Worked like a charm. You just saved me several hours of work... thanks a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment