-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.php
132 lines (117 loc) · 4.23 KB
/
auth.php
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
125
126
127
128
129
130
131
132
<?php
require 'vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfig('client_credentials.json');
$client->addScope(Google\Service\Drive::DRIVE);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
if (!isset($_GET['code'])) {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
} else {
try {
if(!isset($_SESSION['data']['access_token'])) {
$accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$_SESSION['data'] = $accessToken;
$token = $accessToken;
} else {
$token = $_SESSION['data']['access_token'];
}
$client->setAccessToken($token);
// Create a Drive service object
$service = new Google_Service_Drive($client);
$pageToken = isset($_GET['pageToken']) ? $_GET['pageToken'] : null;
// Print the names and IDs for up to 10 files
$optParams = array(
'pageSize' => 10,
'pageToken' => $pageToken,
'fields' => 'nextPageToken, files(id, name, mimeType, description, createdTime, modifiedTime, size, webViewLink, webContentLink, owners, parents, shared, permissions)'
);
// Execute the request
$results = $service->files->listFiles($optParams);
// Start HTML output
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Drive Files</title>
</head>
<body>
<h1>Google Drive Files</h1>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>ID</th>
<th>Mime Type</th>
<th>Description</th>
<th>Created Time</th>
<th>Modified Time</th>
<th>Size</th>
<th>Web View Link</th>
<th>Web Content Link</th>
<th>Owners</th>
<th>Parents</th>
<th>Shared</th>
<th>Permissions</th>
</tr>
</thead>
<tbody>';
// Check if there are files
if (count($results->getFiles()) == 0) {
echo "<tr><td colspan='13'>No files found.</td></tr>";
} else {
$nomor = 0;
// Iterate over files and create table rows
foreach ($results->getFiles() as $file) {
echo '<tr>';
echo '<td>' . ++$nomor . '</td>';
echo '<td>' . htmlspecialchars($file->getName()) . '</td>';
echo '<td>' . htmlspecialchars($file->getId()) . '</td>';
echo '<td>' . htmlspecialchars($file->getMimeType()) . '</td>';
echo '<td>' . htmlspecialchars($file->getDescription()) . '</td>';
echo '<td>' . htmlspecialchars($file->getCreatedTime()) . '</td>';
echo '<td>' . htmlspecialchars($file->getModifiedTime()) . '</td>';
echo '<td>' . htmlspecialchars($file->getSize()) . '</td>';
echo '<td><a href="' . htmlspecialchars($file->getWebViewLink()) . '" target="_blank">View</a></td>';
echo '<td><a href="' . htmlspecialchars($file->getWebContentLink()) . '" target="_blank">Download</a></td>';
// Owners
$owners = $file->getOwners();
$ownerNames = array();
foreach ($owners as $owner) {
$ownerNames[] = htmlspecialchars($owner->getDisplayName());
}
echo '<td>' . implode(", ", $ownerNames) . '</td>';
// Parents
$parents = $file->getParents();
echo '<td>' . ($parents ? implode(", ", $parents) : 'None') . '</td>';
// Shared
echo '<td>' . ($file->getShared() ? 'Yes' : 'No') . '</td>';
// Permissions
$permissions = $file->getPermissions();
$permissionRoles = array();
foreach ($permissions as $permission) {
$permissionRoles[] = htmlspecialchars($permission->getRole());
}
echo '<td>' . implode(", ", $permissionRoles) . '</td>';
echo '</tr>';
}
}
// End HTML output
echo '</tbody>
</table>
</body>
</html>';
// Pagination links
if ($results->getNextPageToken()) {
echo '<a href="?pageToken=' . $results->getNextPageToken() . '">Next Page - '.$results->getNextPageToken().'</a>';
}
} catch (Exception $e) {
echo 'Error during authentication: ' . $e->getMessage();
error_log($e->getMessage());
}
}
?>