mixiアプリ » 技術仕様(RESTful API方式) » PC » RESTful API for PC » RESTful API サンプルコード » Person & Friends API
Person & Friends API
ユーザのプロフィール情報を取得するサンプルです。
PHP
<?php require_once('OAuth.php'); // Establish an OAuth consumer based on our admin 'credentials' $CONSUMER_KEY = 'YOUR_CONSUMER_KEY'; $CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET'; $consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL); // Setup OAuth request based our previous credentials and query $user= '00000000'; $base_feed = 'http://api.mixi-platform.com/os/0.8/people/@me/@self'; $params = array('xoauth_requestor_id' => $user); $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'GET', $base_feed, $params); // Sign the constructed OAuth request using HMAC-SHA1 $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL); // Make signed OAuth request to the Contacts API server $url = $base_feed . '?' . implode_assoc('=', '&', $params); $curl = curl_init($url); 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'); $auth_header = $request->to_header(); if ($auth_header) { curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header)); } $response = curl_exec($curl); if (!$response) { $response = curl_error($curl); } curl_close($curl); var_dump($response); /** * Joins key:value pairs by inner_glue and each pair together by outer_glue * @param string $inner_glue The HTTP method (GET, POST, PUT, DELETE) * @param string $outer_glue Full URL of the resource to access * @param array $array Associative array of query parameters * @return string Urlencoded string of query parameters */ function implode_assoc($inner_glue, $outer_glue, $array) { $output = array(); foreach($array as $key => $item) { $output[] = $key . $inner_glue . urlencode($item); } return implode($outer_glue, $output); } ?>
※古いバージョンの OAuth.php には不具合が発見されています。
上記のサンプルプログラムがうまく動作しない場合は、最新版をダウンロードして試してみてください。
Python
#!/usr/bin/env python import urllib, urllib2 import oauth CONSUMER_KEY = 'YOUR_CONSUMER_KEY' CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET' BASE_URL = 'http://api.mixi-platform.com/os/0.8' # Setup 2 legged OAuth consumer based consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET) params = {'xoauth_requestor_id': 'xxxxxx'} request = oauth.OAuthRequest.from_consumer_and_token( consumer, http_method='GET', http_url=BASE_URL + '/people/@me/@self', parameters=params, ) request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, None) # Query the user's personal info and print them uri = '%s?%s' % (request.http_url, urllib.urlencode(params)) r = urllib2.Request(uri, headers=request.to_header()) try: print urllib2.urlopen(r).read() except urllib2.HTTPError, e: print e
Perl
use 5.0.8; use strict; use warnings; use Data::Dumper; use JSON::XS; use OAuth::Lite; use OAuth::Lite::Consumer; my %options = ( site => 'api.mixi-platform.com', consumer_key => 'YOUR_CONSUMER_KEY', consumer_secret => 'YOUR_CONSUMER_SECRET', requester_id => '00000000', ); my $consumer = OAuth::Lite::Consumer->new( consumer_key => $options{consumer_key}, consumer_secret => $options{consumer_secret} ); my $response = $consumer->request( method => 'GET', url => sprintf('http://%s/os/0.8/people/@me/@self', $options{site}), params => { xoauth_requestor_id => $options{requester_id}, }); if ($response->is_success) { print Dumper(JSON::XS::decode_json($response->decoded_content)); } else { warn $response->status_line; }