跳到主要内容

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"
}