Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiba-yu36/543217 to your computer and use it in GitHub Desktop.
Save shiba-yu36/543217 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use String::Random;
use DateTime;
use URI::Escape;
use Digest::HMAC_SHA1 qw(hmac_sha1);
use MIME::Base64;
use LWP::UserAgent;
my $realm = 'devquiz'; # realm
my $param = { hello => 'world' }; # 送りたいparameter
my $url = 'http://gdd-2010-quiz-japan.appspot.com/oauth/hogehogehoge'; # 送りたいurl
my $oauth_consumer_secret = 'hogehogehoge'; # 秘密鍵
my $oauth_param = {
oauth_consumer_key => 'hogehogehoge',
oauth_nonce => String::Random->new->randregex('\w{20}'),
oauth_timestamp => DateTime->now->epoch,
oauth_version => '1.0',
oauth_signature_method => 'HMAC-SHA1'
};
# Authorizationヘッダの作成
my $header = join(', ', map {
$_ . '=' . '"' . $oauth_param->{$_} . '"';
} keys %$oauth_param);
$header = qq{OAuth realm="${realm}", } . $header;
# signatureの為のベース文字列を作成
my $signature_string = "POST&" . uri_escape($url);
my $signature_param = {
%$oauth_param,
%$param,
};
my $parameter_string .= join('&', map {
$_ . '=' . $signature_param->{$_};
} sort keys %$signature_param);
$signature_string .= '&' . uri_escape($parameter_string);
# signatureの作成
my $hmac_sha1_key = $oauth_consumer_secret . '&';
$signature_string = hmac_sha1($signature_string, $hmac_sha1_key);
$signature_string = encode_base64($signature_string);
chomp $signature_string;
# headerへ代入
$header .= ', oauth_signature="' . $signature_string . '"';
# 送信
my $ua = LWP::UserAgent->new;
my $res = $ua->post($url, $param, 'Authorization' => $header);
use YAML;
warn YAML::Dump($res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment