-
Notifications
You must be signed in to change notification settings - Fork 9
/
client_example.php
50 lines (49 loc) · 1.85 KB
/
client_example.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
<?php
session_start();
if (!isset($_GET['code']) && !isset($_SESSION['token'])) {
$param = [
'client_id' => '3',
'redirect_uri' => 'redirect_uri=https://yourapp.com/api/callback',
'response_type' => 'code',
'scope' => 'user profile',
];
http_redirect('https://ldap.tp.edu.tw/oauth/authorize', $param);
exit;
} elseif (!isset($_SESSION['token'])) {
$param = [
'grant_type' => 'authorization_code',
'client_id' => '3',
'client_secret' => '5uyghc0DpeRJHsv43Di567fjasuy083kf6hiDAT',
'redirect_uri' => 'redirect_uri=https://yourapp.com/api/callback',
'code' => _GET[‘code’],
];
$response = http_post_fields('https://ldap.tp.edu.tw/oauth/token', $param);
$token = json_decode($response);
ini_set("session.gc_maxlifetime", $token->expires_in);
$_SESSION['token'] = $token->access_token;
$_SESSION['refresh'] = $token->refresh_token;
$_SESSION['expire'] = time() + $token->expires_in;
} elseif (time() > $_SESSION['expire']) {
$param = [
'grant_type' => 'refresh_token',
'refresh_token' => $_SESSION['refresh'],
'client_id' => '3',
'client_secret' => '5uyghc0DpeRJHsv43Di567fjasuy083kf6hiDAT',
'scope' => 'user profile',
];
$response = http_post_fields('https://ldap.tp.edu.tw/oauth/token', $param);
$token = json_decode($response);
$_SESSION['token'] = $token->access_token;
$_SESSION['refresh'] = $token->refresh_token;
$_SESSION['expire'] = time() + $token->expires_in;
}
$header = [
'Authorization' => 'Bearer '.$_SESSION['token'],
];
$response = http_get('https://ldap.tp.edu.tw/api/user', $header);
$user = json_decode($response);
$response = http_get('https://ldap.tp.edu.tw/api/profile', $header);
$profile = json_decode($response);
//logout
http_redirect('https://ldap.tp.edu.tw/api/logout');
?>