-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetFile.php
More file actions
115 lines (100 loc) · 3.59 KB
/
getFile.php
File metadata and controls
115 lines (100 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
error_reporting(E_ALL & ~E_NOTICE);
//session_start();
//if (!$_SESSION["isAdmin"]){
// header('Location: /401.php');
// exit();
//}
require 'vendor/autoload.php';
//$fileId = $_GET["fileId"];
define('APPLICATION_NAME', 'Drive API PHP Quickstart');
//define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', '../../client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Drive::DRIVE_READONLY)
));
$fileId = $argv[1];
$debug = false;
echo getFile($fileId);
//if (php_sapi_name() != 'cli') {
// throw new Exception('This application must be run on the command line.');
//}
function getFile($fileId) {
if ($debug) { echo "Got to stage 1\n";}
$client = getClient();
$service = new Google_Service_Drive($client);
if ($debug) { echo "Got to stage 2\n";}
$file = $service->files->get($fileId);
$url = $file->getExportLinks()["text/csv"];
if ($debug) { echo "Got a download url of " . $url . "<br/>\n";}
if ($url) {
return downloadFile($client,$url);
//header('Content-Type: text/csv');
//header('Content-disposition: filename="data.csv"');
//echo downloadFile($client,$url);
}
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
if ($debug) { echo "get client <br/>\n";}
$client = new Google_Client();
if ($debug) { echo "set a client <br/>\n"; }
$client->setApplicationName(APPLICATION_NAME);
if ($debug) { echo "set application name <br/>\n";}
$client->setScopes(SCOPES);
if ($debug) { echo "set scopes <br/>\n";}
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
if ($debug) { echo "set client secret <br/>\n";}
$client->setAccessType('offline');
if ($debug) { echo "set access type <br/>\n";}
// Load previously authorized credentials from a file.
$accessToken = getenv("ODI_DRIVE_TOKEN");
if ($debug) { echo "got access token : " . $accessToken . "<br/>\n";}
if ($accessToken != "") {
} else {
echo "No access token, please speak to a site admin!";
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
if ($debug) { echo "set access token <br/>\n";}
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
if ($debug) { echo "need refresh token <br/>\n";}
$client->refreshToken($client->getRefreshToken());
if ($debug) { echo "got refrest token <br/>\n";}
putenv("ODI_DRIVE_TOKEN=".$client->getAccessToken());
if ($debug) { echo "set access token<br/>\n";}
}
return $client;
}
function downloadFile($service, $downloadUrl) {
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$httpRequest = $service->getAuth()->authenticatedRequest($request);
//echo $httpRequest->getResponseHttpCode();
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}