-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.php
More file actions
121 lines (102 loc) · 3.73 KB
/
quickstart.php
File metadata and controls
121 lines (102 loc) · 3.73 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
116
117
118
119
120
121
<?php
session_start();
include_once('funciones.php');
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('486485021619.apps.googleusercontent.com');
$client->setClientSecret('_Vog54Zff_Gh6flNn1ZlrsUs');
//La comento porque trabajo en local
//$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setRedirectUri('http://localhost/quickstart.php');
$client->setScopes(array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.apps.readonly'));
//autentificacion
$client->setUseObjects(true);
if (isset($_GET['code'])) {
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
//$client->setAccessToken($_SESSION['accessToken']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$token=$client->authenticate();
$client->setAccessToken($token);
}
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
// initialize the drive service with the client.
// $result = array();
//$files = $service->files->listFiles();
// $result = array_merge($result, $files->getItems());
//print_r($result[0]->getItems());
$vector=retrieveAllFiles($service);
//print_r($vector[0]->getTitle());
print $vector[0]->getDownloadUrl();// este comando obtiene el enlace de descarga
foreach ($vector as $file) {
print "Title: " . $file->getTitle() ."<br/>";
print "Description: " . $file->getDescription()."<br/>";
print "MIME type: " . $file->getMimeType()."<br/>";
print "<a href='" . $file->getDownloadUrl() ."'>Descargar</a><br>";
}
//-----------------
/*
$service = new Google_DriveService($client);
$app->get('/svc', function() use ($app, $client, $service) {
checkUserAuthentication($app);
checkRequiredQueryParams($app, array('file_id'));
$fileId = $app->request()->get('file_id');
try {
// Retrieve metadata for the file specified by $fileId.
$file = $service->files->get($fileId);
// Get the contents of the file.
$request = new Google_HttpRequest($file->downloadUrl);
$response = $client->getIo()->authenticatedRequest($request);
$file->content = $response->getResponseBody();
renderJson($app, $file);
} catch (Exception $ex) {
renderEx($app, $ex);
}
});
*/
//lista archivos del server
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
$dir->close();
//sube un archivo
if (!empty($_POST)) {
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = finfo_file($finfo, $file_path);
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
}
finfo_close($finfo);
header('location:'.$url);exit;
}
include 'index.phtml';