メインコンテンツまでスキップ

Client Credentials Grant での People API へのリクエスト

概要​

ここでは Client Credentials Grant で取得した Access Token を使った People API の呼び出し方法を説明していきます。

People API の詳細はこちらを参照してください。

Client Credentials Grant についてはこちらを参照してください。

People API を使ったユーザー情報の取得​

People API を使う際には下記の手順が必要になります。

  • Client Credentials Grant で Access Token を取得
  • 取得した AccessToken を使って People API へリクエストを送信

Client Credentials Grant での Accsess Token の取得​

Token Endpoint へリクエストし Access Token を取得します。

警告

Client Credential Grant で取得した Access Token は特定のユーザーに紐づく物ではなく Client のみに紐づく権限ですので、ユーザーの認証が必要な RESTful API へのリクエストには使えません。

各 RESTful API での Client Credentials Grant のサポートについては RESTful API の使い方ページを確認してください。

下記は Access Token の取得処理のサンプルコードになります。

<?php
define('ERROR_LOG_PATH', '/var/log/php/error.log');
define('TOKEN_ENDPOINT_URL', 'https://sb-connect.mobage.jp/connect/1.0/api/token');
define('PEOPLE_END_POINT', 'https://sb-app.mobage.jp/social/api/restful/v2/people');
define('CLIENT_ID', 'YOUR_CLIENT_ID');
define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');
define('TEST_USER_ID', 'YOUR_TEST_USER_ID');
 
$token_endpoint_request_params = [
'grant_type' => 'client_credentials',
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET
];
$headers = [
'Content-Type: application/x-www-form-urlencoded',
'Connection: close',
];
$options = [
'http' => [
'method' => 'POST',
'content' => http_build_query($token_endpoint_request_params),
'header' => implode("\r\n", $headers),
]
];
$raw_token_response = file_get_contents(TOKEN_ENDPOINT_URL, false, stream_context_create($options));

$token_response = json_decode($raw_token_response);
error_log('token_response ' . print_r($token_response,true) . "\n", 3, ERROR_LOG_PATH);

$client_access_token = $token_response->access_token;
$expires_time = time() + $token_response->expires_in;

取得した Access Token を使って People API へリクエストを送信​

Client Credentials Grant で取得した Access Token を使って Authentication Header 生成し People API へリクエストを行います。

下記のサンプルコードではクエリパラメータの fields で、取得したいプロフィール属性情報を指定してしています。

※ fields には RESTful API の People に記載されている People オブジェクトフィールドの値を複数指定可能です。

// Request People API
$u_id = TEST_USER_ID;
$http_method = "GET";
$url_fragment = "/{$u_id}/@self";
$query_parameters = '?fields=id,nickname,profileUrl,thumbnailUrl&format=json';

// generate Authentication Header
$endpoint = PEOPLE_END_POINT . $url_fragment . $query_parameters;
$auth_header = array(
"Authorization: Bearer " . $client_access_token,
"Accept: */*",
"Content-Type: application/json; charset=utf-8"
);
// access to platform server
$curl = curl_init($endpoint);
$is_post_method = ($http_method == 'GET') ? true : false;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_ENCODING , "gzip");
curl_setopt($curl, CURLOPT_HTTPHEADER, $auth_header);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);

// request header
$curl_header_req = curl_getinfo($curl, CURLINFO_HEADER_OUT);
// response header and body
$curl_header_res_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$curl_header_res = substr($response, 0, $curl_header_res_size);
$curl_body_res = substr($response, $curl_header_res_size);
$http_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

// Check Response from API Server
if ($http_status_code != 200) {
// Respond Error Page
print("<html><body><p>Got an error from API Server ($http_status_code, $curl_body_res)</p></body></html>");
exit ;
} else {
print("<html><body><p>Successfully $curl_body_res </p></body></html>");
}

?>

レスポンスについて​

リクエストが成功すると、リクエストの際に指定した fields に対応した値がレスポンスに含まれます。

下記に正常な People API からのレスポンスの例を記載します。

{
"nickname" : "name",
"thumbnailUrl" : "https://sb-sp.mbga.jp/img_u/xxxxx/0.1.gif",
"id" : "sb.mbga.jp:xxxxx",
"profileUrl" : "https://sb-sp.mbga.jp/_u?u=xxxxx"
}