getToken.php 1.64 KB
<?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;
?>