getToken.php
1.64 KB
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
<?php
require_once('./vendor/autoload.php');
use \Firebase\JWT\JWT;
$url = 'https://api.researchmap.jp/oauth2/token';
$client_key = file_get_contents('/var/www/.rmap_keys/rmap_client_id.key');
$client_key = rtrim($client_key);
$private_key = file_get_contents('/var/www/.rmap_keys/rmap_jwt_private.key');
# 時刻
$now = time();
$hour = date('H', $now);
$min = date('m', $now);
$sec = date('s', $now);
$year = date('Y', $now);
$month = date('m', $now);
$day = date('d', $now);
$date = mktime($hour, $min, $sec, $month, $day, $year);
$expiration = mktime(0, 0, 0, $month, $day+1, $year);
$claim = Array(
'iss' => $client_key,
'aud' => $url,
'sub' => "0",
'exp' => $expiration,
'iat' => $date
);
$jwt = JWT::encode($claim, $private_key, 'RS256');
$data = Array(
"grant_type" => "urn:ietf:params:oauth:grant-type:jwt-bearer",
"assertion" => $jwt,
// "scope" => "researchers write",
"version" => "2"
);
$header = array(
"Content-Type: application/x-www-form-urlencoded;"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'tmp');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$api_responese = curl_exec($curl);
header('Content-Type: application/json; charset=utf8');
header('Access-Control-Allow-Origin: *');
echo $api_responese;
?>