Last active
September 8, 2022 00:42
-
-
Save printminion/6624985accb5570aab2d to your computer and use it in GitHub Desktop.
Download google drive spreadheet as csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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/"] | |
} | |
} | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked like a charm. You just saved me several hours of work... thanks a ton!