Permalink
Browse files

added Perl example

  • Loading branch information...
1 parent 3736f75 commit 8f3d174f98d63fdc1dc37c5fb5167202f3fdd766 @chipple chipple committed Oct 20, 2011
Showing with 117 additions and 0 deletions.
  1. +12 −0 Perl/README.md
  2. +105 −0 Perl/sample.pl
View
@@ -0,0 +1,12 @@
+# 実行手順
+## コードの書き換え
+コード内のCONSUMER_KEY, CONSUMER_SECRET, REDIRECT_URIを適切に書き換えてください。
+## Authorization codeの入手
+以下のURLにWebブラウザにてアクセスし、リダイレクト先に含まれるcodeパラメータ値を入手する。
+
+ https://mixi.jp/connect_authorize.pl?client_id=[YOUR CONSUMER KEY]&scope=r_profile
+
+## 実行
+
+ $ sample.pl [codeパラメータ値]
+
View
@@ -0,0 +1,105 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use constant {
+ CONSUMER_KEY => '[YOUR CONSUMER KEY]',
+ CONSUMER_SECRET => '[YOUR CONSUMER SECRET]',
+ REDIRECT_URI => '[YOUR REDIRECT URI]',
+
+ MIXI_TOKEN_ENDPOINT => 'https://secure.mixi-platform.com/2/token',
+ MIXI_API_ENDPOINT => 'http://api.mixi-platform.com/2'
+};
+
+use URI::Escape qw/ uri_escape /;
+use HTTP::Request;
+use LWP::UserAgent;
+use JSON::XS;
+use Data::Dumper;
+
+sub request {
+ my ($method, $url, $data_arr) = @_;
+
+ my $req = HTTP::Request->new(
+ $method => $url
+ );
+
+ if ($method eq 'POST') {
+ my $data_str = join '&', map { uri_escape($_) . '=' . uri_escape($data_arr->{$_}) } keys %$data_arr;
+ $req->content_type('application/x-www-form-urlencoded');
+ $req->content($data_str);
+ }
+
+ my $ua = LWP::UserAgent->new();
+ my $res = $ua->request($req);
+
+ return $res;
+}
+
+sub get_token {
+ my $auth_code = shift;
+
+ my %data_arr = (
+ 'grant_type' => 'authorization_code',
+ 'client_id' => CONSUMER_KEY,
+ 'client_secret' => CONSUMER_SECRET,
+ 'code' => $auth_code,
+ 'redirect_uri' => REDIRECT_URI,
+ );
+
+ my $res = request('POST', MIXI_TOKEN_ENDPOINT, \%data_arr);
+ die 'Request failed. ' . $res->status_line unless $res->is_success;
+
+ return decode_json($res->content);
+}
+
+sub get_new_token {
+ my $refresh_token = shift;
+
+ my %data_arr = (
+ 'grant_type' => 'refresh_token',
+ 'client_id' => CONSUMER_KEY,
+ 'client_secret' => CONSUMER_SECRET,
+ 'refresh_token' => $refresh_token,
+ );
+
+ my $res = request('POST', MIXI_TOKEN_ENDPOINT, \%data_arr);
+ die 'Request failed. ' . $res->status_line unless $res->is_success;
+
+ return decode_json($res->content);
+}
+
+sub call {
+ my ($endpoint, $token) = @_;
+
+ my $url = MIXI_API_ENDPOINT . $endpoint . '?oauth_token=' . uri_escape($token->{'access_token'});
+ my $res = request('GET', $url);
+
+ if (defined $res->header('WWW-Authenticate')) {
+ my $error_msg = $res->header('WWW-Authenticate');
+
+ if ($error_msg =~ /invalid_request/) {
+ die 'Invalid request.';
+ } elsif ($error_msg =~ /invalid_token/) {
+ die 'Invalid token.';
+ } elsif ($error_msg =~ /expired_token/) {
+ $token = get_new_token($token->{'refresh_token'});
+ $url = MIXI_API_ENDPOINT . $endpoint . '?oauth_token=' . uri_escape($token->{'access_token'});
+ return decode_json(request('GET', $url)->{'_content'});
+ } elsif ($error_msg =~ /insufficient_scope/) {
+ die 'Insufficient scope.';
+ }
+ }
+
+ return decode_json($res->content);
+}
+
+my $auth_code = $ARGV[0];
+my $token = get_token($auth_code);
+my $json_href = call('/people/@me/@self', $token);
+
+print Data::Dumper::Dumper($json_href);
+
+1;
+

0 comments on commit 8f3d174

Please sign in to comment.