-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,29 @@ | ||
<?php | ||
require_once '../vendor/autoload.php'; | ||
|
||
use HMS\AccountKit\AccountKit; | ||
use HMS\DriveKit\DriveKit; | ||
|
||
$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) { | ||
// convert the expiry timestamp from relative to absolute value. | ||
file_put_contents($token_path, $token_response); | ||
} | ||
} else if (file_exists($token_path) && filesize($token_path) > 2) { | ||
|
||
// load previously authorized token from a file, if it exists. | ||
$token_response = json_decode(file_get_contents($token_path), true); | ||
|
||
// determine token expiry and perform a refresh, when required. | ||
if ($token_response->expiry >= time() && property_exists($token_response, 'refresh_token')) { | ||
$token_response = $api->get_access_token_by_refresh_token( $token_response->refresh_token ); | ||
} | ||
} | ||
include './oauth2.php'; | ||
?> | ||
<html lang="en"> | ||
<head> | ||
<title>DriveKit Example</title> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | ||
<script type="text/javascript">function redirect() { | ||
window.location.href = '<?= $api->get_login_url(); ?>'; | ||
window.location.href = '<?= /** @var \HMS\AccountKit\ $api */ $api->get_login_url(); ?>'; | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<?php | ||
if (isset( $token_response )) { | ||
if (isset( $token_response ) && property_exists($token_response, 'access_token')) { | ||
$drive = new DriveKit( ['access_token' => $token_response->access_token] ); | ||
$result = $drive->getAbout()->get(); | ||
die('<pre>' . print_r($result, true) . '</pre>'); | ||
} else { ?> | ||
<p><button onclick=redirect()>Login with HUAWEI ID</button></p> | ||
<?php } ?> | ||
echo '<pre>' . print_r($result, true) . '</pre>'; | ||
if ($result->code == 401) { | ||
echo '<p><button onclick=redirect()>Login with HUAWEI ID</button></p>'; | ||
} | ||
} else { | ||
echo '<p><button onclick=redirect()>Login with HUAWEI ID</button></p>'; | ||
} | ||
?> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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) { | ||
// convert the expiry timestamp from relative to absolute value. | ||
file_put_contents($token_path, $token_response); | ||
} | ||
} else if (file_exists($token_path) && filesize($token_path) > 2) { | ||
|
||
// 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 ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
require_once '../vendor/autoload.php'; | ||
use HMS\SearchKit\SearchKit; | ||
include './oauth2.php'; | ||
?> | ||
<html lang="en"> | ||
<head> | ||
<title>SearchKit Example</title> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | ||
<script type="text/javascript">function redirect() { | ||
window.location.href = '<?= /** @var \HMS\AccountKit\ $api */ $api->get_login_url(); ?>'; | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<?php | ||
if (isset( $token_response ) && property_exists($token_response, 'access_token')) { | ||
$search = new SearchKit( ['access_token' => $token_response->access_token] ); | ||
$result = $search->web_search('test search'); | ||
echo '<pre>' . print_r($result, true) . '</pre>'; | ||
if ($result->code == 401) { | ||
echo '<p><button onclick=redirect()>Login with HUAWEI ID</button></p>'; | ||
} | ||
} else { | ||
echo '<p><button onclick=redirect()>Login with HUAWEI ID</button></p>'; | ||
} | ||
?> | ||
</body> | ||
</html> |