-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivekit.php
65 lines (61 loc) · 2.88 KB
/
drivekit.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
<?php
require_once '../vendor/autoload.php';
use HMS\AccountKit\AccountKit;
use HMS\DriveKit\DriveKit;
// appending 'redirect_drivekit' to the $oauth2_redirect_url.
$oauth2_redirect = 'redirect_drivekit';
if (isset($_SERVER['HUAWEI_OAUTH2_REDIRECT_URL']) && ! str_contains($_SERVER['HUAWEI_OAUTH2_REDIRECT_URL'], $oauth2_redirect)) {
$_SERVER['HUAWEI_OAUTH2_REDIRECT_URL'] = $_SERVER['HUAWEI_OAUTH2_REDIRECT_URL'] . $oauth2_redirect;
}
// appending 'drive' (read/write) to $oauth2_api_scope.
$oauth2_scope = 'https://www.huawei.com/auth/drive';
if (isset($_SERVER['HUAWEI_OAUTH2_API_SCOPE']) && ! str_contains($_SERVER['HUAWEI_OAUTH2_API_SCOPE'], $oauth2_scope)) {
$_SERVER['HUAWEI_OAUTH2_API_SCOPE'] = $_SERVER['HUAWEI_OAUTH2_API_SCOPE'] . ' ' . $oauth2_scope;
}
include './oauth2.php';
?>
<html lang="en">
<head>
<title>DriveKit Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
div.image_png {background-color: #E1E1E1;}
</style>
<script type="text/javascript">function redirect() {
window.location.href = '<?= /** @var AccountKit $api
* @noinspection PhpRedundantVariableDocTypeInspection
*/ $api->get_login_url(); ?>';
}
</script>
</head>
<body>
<div>
<?php
if (isset($error)) {echo $error;}
if (isset( $token_response ) && property_exists($token_response, 'access_token')) {
$drive = new DriveKit( ['access_token' => $token_response->access_token] );
$result = $drive->getAbout()->get();
if (property_exists($result, 'code') && $result->code == 401) {
echo '<p><button onclick=redirect()>Login with HUAWEI ID</button></p>';
} else {
// About
echo '<div>Domain: '. $result->domain . '</div>';
echo '<div>Permission ID: '. $result->user->permissionId . '</div>';
echo '<div>User: '. $result->user->displayName . '</div>';
echo '<div>Capacity: '. format_filesize($result->storageQuota->userCapacity) . '</div>';
echo '<div>Used: ' . format_filesize($result->storageQuota->usedSpace) . '</div>';
// List
$result = $drive->getFiles()->list();
foreach ($result->files as $file) {
echo '<div class="'.str_replace('/','_', $file->mimeType).'">'.$file->mimeType.' '.format_filesize($file->size).' '.$file->fileName.'</div>';
}
if (property_exists($result, 'nextCursor')) {}
// $drive->getFiles()->delete_by_name('');
}
} else {
echo '<p><button onclick=redirect()>Login with HUAWEI ID</button></p>';
}
?>
</div>
</body>
</html>