-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrive.php
More file actions
124 lines (93 loc) · 3.11 KB
/
Copy pathDrive.php
File metadata and controls
124 lines (93 loc) · 3.11 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
122
123
124
<?php
require_once 'vendor/autoload.php';
class Drive
{
private $client = null;
private $drive_service = null;
function __construct()
{
$this->client = new Google_Client();
$this->client->setIncludeGrantedScopes(true);
//solicitar acceso
$this->client->addScope('https://www.googleapis.com/auth/drive');
//$this->client->setAccessType('offline');
$this->client->setAuthConfigFile('client_secrets.json');
}
//Creamos un service object de la api que vamos a utilizar
public function create_google_service_api ()
{
$this->drive_service = new Google_Service_Drive($this->client);
}
public function client(){
return $this->client;
}
// Autenticacion en OAuth 2.0 de googles
public function get_credentials(){
// 3) Generar una URL para solicitar el acceso desde el servidor de OAuth 2.0 de Google:
$auth_url = $this->client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
// Establecemos la pagina de redireccion luego de la autenticacion
public function set_redirect($arch){
$this->client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] .'/'.$arch);
}
public function Auth($code){
if (!isset($_SESSION['access_token'])) {
// Dado el código de autorización a un token de acceso
$this->client->authenticate($code);
// Guardamos el codigo de acceso en la session
$_SESSION['access_token'] = $this->client->getAccessToken();
}
$this->client->setAccessToken($_SESSION['access_token']);
}
/* Se realiza la peticion a la api: listamos los archivos
de google drive */
public function get_list_files(){
$optParams = array(
'fields' => 'nextPageToken, files(id, name)',
'q' => "'root' in parents");
$files_list=$this->drive_service->files->listFiles($optParams);
$result= array();
if (count($files_list->getFiles()) == 0) {
return array_push($result,"No files fount");
}
else {
foreach ($files_list->getFiles() as $file) {
array_push($result,['name'=>$file->getName(),'id'=> $file->getId()]);
}
}
return $result;
}
public function create_doc($title){
$file = new Google_Service_Drive_DriveFile();
$file->setName($title);
$file->setMimeType('application/vnd.google-apps.document');
$result = $this->drive_service->files->create($file, array());
}
public function shared_file($fileId,$mail){
$this->drive_service->getClient()->setUseBatch(true);
try {
$batch = $this->drive_service->createBatch();
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'user',
'role' => 'writer',
'emailAddress' => $mail
));
$request = $this->drive_service->permissions->create(
$fileId, $userPermission, array('fields' => 'id'));
$batch->add($request, 'user');
$results = $batch->execute();
foreach ($results as $result) {
if ($result instanceof Google_Service_Exception) {
// Handle error
printf($result);
} else {
printf("Permission ID: %s\n", $result->id);
}
}
} finally {
$this->drive_service->getClient()->setUseBatch(false);
}
}
}
?>