-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2.php
55 lines (48 loc) · 2.76 KB
/
oauth2.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
<?php
require_once '../vendor/autoload.php';
use HMS\AccountKit\AccountKit;
$token_path = '../../.credentials/huawei_token.json';
if (! is_writeable($token_path)) {die( 'unable to cache the token: '.$token_path );}
if (! isset($_SERVER['HUAWEI_OAUTH2_CLIENT_ID'])) {die( 'missing variable: HUAWEI_OAUTH2_CLIENT_ID' );}
if (! isset($_SERVER['HUAWEI_OAUTH2_CLIENT_SECRET'])) {die( 'missing variable: HUAWEI_OAUTH2_CLIENT_SECRET' );}
if (! isset($_SERVER['HUAWEI_OAUTH2_REDIRECT_URL'])) {die( 'missing variable: HUAWEI_OAUTH2_REDIRECT_URL' );}
if (! isset($_SERVER['HUAWEI_OAUTH2_API_SCOPE'])) {die( 'missing variable: HUAWEI_OAUTH2_API_SCOPE' );}
$api = new AccountKit( [
'oauth2_client_id' => $_SERVER['HUAWEI_OAUTH2_CLIENT_ID'],
'oauth2_client_secret' => $_SERVER['HUAWEI_OAUTH2_CLIENT_SECRET'],
'oauth2_redirect_url' => $_SERVER['HUAWEI_OAUTH2_REDIRECT_URL'],
'oauth2_api_scope' => $_SERVER['HUAWEI_OAUTH2_API_SCOPE'],
'debug_mode' => true
]);
// https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/web-get-access-token-0000001050048946#section151118514311
if (isset($_GET['code'])) {
$token_response = $api->get_access_token_by_auth_code( $_GET['code'] );
if ($token_response != null) {
if (property_exists($token_response, 'code') && property_exists($token_response, 'message')) {
$error = 'Error ' . $token_response->code .' / '. $token_response->message;
} else {
// convert the expiry timestamp from relative to absolute value.
file_put_contents($token_path, json_encode($token_response));
}
}
} else if (file_exists($token_path) && filesize($token_path) > 0) {
// load previously authorized token from a file, if it exists.
$token_response = (object) json_decode(file_get_contents($token_path), true);
// determine token expiry and perform a refresh, when required.
if (property_exists($token_response, 'expiry')) {
if ($token_response->expiry >= time() && property_exists($token_response, 'refresh_token')) {
$token_response = $api->get_access_token_by_refresh_token( $token_response->refresh_token );
if (property_exists($token_response, 'access_token')) {
file_put_contents($token_path, json_encode($token_response));
}
}
}
}
function format_filesize(int $bytes, int $decimals=2): string {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
}
if (isset($_GET['error']) && isset($_GET['sub_error']) && $_GET['error_description']) {
$error = 'Error ' . $_GET['error'] .' / '. $_GET['sub_error'] . ' ' . $_GET['error_description'];
}