跳到主要内容

RESTful API の使い方

PC版 Mobageオープンプラットフォームでも、パートナーサーバーからの API 呼び出し手段として RESTful API を用意しています。
以下のものを PC版として提供しています。

RESTful API のリクエストには OAuth Signature を付ける必要があります。また、この際の xoauth_requestor_id はアプリケーションIDを指定してください。
OAuth Signature の詳しい生成方法はこちらをご覧ください。

サンプル​

People API を使ってあるユーザーのプロフィール情報を取得するには以下のようにします。
Perl: OAuth::Lite モジュールは 1.24 以上を利用してください

use strict;
use warnings;
use OAuth::Lite::Consumer;
use JSON;
my $consumer_key = 'YOUR CONSUMER KEY';
my $consumer_secret = 'YOUR CONSUMER SECRET';
my $app_id = 00000000; # YOUR APPLICATION ID
my $user_id = 00000000; # YOUR USER ID
my $base_url = 'http://app.sb.mbga-platform.jp/social/api/restful/v2';
my $url = "$base_url/people/$user_id/\@self";
my $method = 'GET';
my $consumer = OAuth::Lite::Consumer->new(
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
);
my $res = $consumer->request(
method => $method,
url => $url,
params => { xoauth_requestor_id => $app_id, },
);
my $person = decode_json( $res->content );

PHP: OAuth ライブラリはここから入手できます。

<?php
require_once 'OAuth.php';
$consumer_key = 'YOUR CONSUMER KEY';
$consumer_secret = 'YOUR CONSUMER SECRET';
$app_id = 00000000; // YOUR APPLICATION ID
$user_id = 00000000; // YOUR USER ID
$base_url = 'http://app.sb.mbga-platform.jp/social/api/restful/v2';
$url = "$base_url/people/$user_id/@self?xoauth_requestor_id=$app_id";
$method = 'GET';
$consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);
parse_str(parse_url($url, PHP_URL_QUERY), $params);
$request = OAuthRequest::from_consumer_and_token($consumer, NULL, $method, $url, $params);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
$auth_header = $request->to_header();
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_ENCODING , "gzip");
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
$response = curl_exec($curl);
curl_close($curl);
$person = json_decode($response);
?>

更新履歴​

  • 2011/2/23
    • OAuth::Lite モジュールについての記述を修正