Skip to content

Commit

Permalink
examples updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
syslogic committed Feb 9, 2023
1 parent 051ffd4 commit bfdc4bb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 66 deletions.
11 changes: 0 additions & 11 deletions .idea/runConfigurations/DriveKit.xml

This file was deleted.

11 changes: 0 additions & 11 deletions .idea/runConfigurations/SearchKit.xml

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ This project aims to abstract Huawei REST API, according to the official API doc
| [`AccountKit`](https://github.com/syslogic/php-hms/blob/master/src/AccountKit) | working |
| [`PushKit`](https://github.com/syslogic/php-hms/blob/master/src/PushKit) | working |
| [`MapKit`](https://github.com/syslogic/php-hms/blob/master/src/MapKit) | working |
| [`LocationKit`](https://github.com/syslogic/php-hms/blob/master/src/LocationKit) | N/A |
| [`LocationKit`](https://github.com/syslogic/php-hms/blob/master/src/LocationKit) | untested |
| [`AdsKit`](https://github.com/syslogic/php-hms/blob/master/src/AdsKit) | working |
| [`AnalyticsKit`](https://github.com/syslogic/php-hms/blob/master/src/AnalyticsKit) | working |
| [`AppGallery\Connect`](https://github.com/syslogic/php-hms/tree/master/src/AppGallery/Connect) | |
| [`AppGallery\Publishing`](https://github.com/syslogic/php-hms/tree/master/src/AppGallery/Publishing) | [plugin](https://github.com/syslogic/agconnect-publishing-gradle-plugin) |
| [`DriveKit`](https://github.com/syslogic/php-hms/tree/master/src/DriveKit) | partially |
| [`GameService`](https://github.com/syslogic/php-hms/tree/master/src/GameService) | |
| [`GameService`](https://github.com/syslogic/php-hms/tree/master/src/GameService) | untested |
| [`SearchKit`](https://github.com/syslogic/php-hms/blob/master/src/SearchKit) | |
| [`CloudSms`](https://github.com/syslogic/php-hms/blob/master/src/CloudSms) | working |
| [`CloudSms`](https://github.com/syslogic/php-hms/blob/master/src/CloudSms) | untested |
| [`WalletKit`](https://github.com/syslogic/php-hms/blob/master/src/WalletKit) | |

[![PHP Composer](https://github.com/syslogic/php-hms/actions/workflows/ci-php.yml/badge.svg)](https://github.com/syslogic/php-hms/actions/workflows/ci-php.yml)
Expand Down
52 changes: 11 additions & 41 deletions www/drivekit.php
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>
39 changes: 39 additions & 0 deletions www/oauth2.php
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 );
}
}
}
29 changes: 29 additions & 0 deletions www/searchkit.php
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>

0 comments on commit bfdc4bb

Please sign in to comment.