$Net::OAuth::PROTOCOL_VERSION
= Net::OAuth::PROTOCOL_VERSION_1_0A;
around
BUILDARGS
=>
sub
{
my
$orig
=
shift
;
my
$class
=
shift
;
my
$args
=
$class
->
$orig
(
@_
);
my
$oauth_urls
= {
};
return
{
%$oauth_urls
,
%$args
};
};
has
[
qw( consumer_key consumer_secret )
] => (
is
=>
'ro'
,
isa
=>
'Str'
,
required
=> 1 );
has
'user_agent'
=> (
is
=>
'ro'
,
isa
=> InstanceOf[
'LWP::UserAgent'
],
lazy
=> 1,
builder
=>
'_build_user_agent'
,
);
for
my
$attribute
(
qw/authorization_url request_token_url access_token_url/
) {
has
$attribute
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
required
=> 1,
reader
=> {
$attribute
=>
sub
{ URI->new(
shift
->{
$attribute
}) } },
);
}
for
my
$attribute
(
qw/access_token access_token_secret request_token request_token_secret/
) {
has
$attribute
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
clearer
=>
"clear_$attribute"
,
predicate
=>
"has_$attribute"
,
);
}
sub
_build_user_agent {
my
$self
=
shift
;
my
$user_agent
= LWP::UserAgent->new();
$user_agent
->env_proxy;
return
$user_agent
;
}
sub
get_authorization_url {
my
(
$self
,
%params
) =
@_
;
my
$callback
=
delete
$params
{callback} ||
'oob'
;
$self
->authorization_request_token(
callback
=>
$callback
);
my
$uri
=
$self
->authorization_url;
$uri
->query_form(
oauth_token
=>
$self
->request_token,
%params
);
return
$uri
;
}
sub
make_oauth_request {
my
(
$self
,
$type
,
%params
) =
@_
;
return
unless
$type
;
my
$proto
= Net::OAuth->request(
$type
);
my
$request
=
$proto
->new(
version
=>
'1.0'
,
consumer_key
=>
$self
->consumer_key,
consumer_secret
=>
$self
->consumer_secret,
request_method
=>
'GET'
,
signature_method
=>
'HMAC-SHA1'
,
timestamp
=>
time
,
nonce
=> Digest::SHA::sha1_base64(
time
. $$ .
rand
),
%params
,
);
$request
->sign;
return
$request
;
}
sub
get_request_token {
my
(
$self
,
$params
) =
@_
;
my
$uri
=
$self
->request_token_url;
my
$request
=
$self
->make_oauth_request(
'request token'
,
request_url
=>
$uri
,
%$params
,
);
my
$http_req
= HTTP::Request->new(
GET
=>
$uri
);
$http_req
->headers->authorization_basic(
$self
->consumer_key,
$self
->consumer_secret );
$http_req
->header(
authorization
=>
$request
->to_authorization_header);
my
$res
=
$self
->user_agent->send_request(
$http_req
);
if
(
$res
->is_success ) {
$uri
->query(
$res
->content);
my
%res_params
=
$uri
->query_form;
return
(
$self
->request_token(
$res_params
{oauth_token}),
$self
->request_token_secret(
$res_params
{oauth_token_secret}),
);
}
else
{
croak
sprintf
(
"Something went wrong on GET %s: %s"
,
$uri
,
$res
->status_line );
}
}
sub
get_access_token {
my
(
$self
,
$params
) =
@_
;
my
$uri
=
$self
->access_token_url;
my
$request
=
$self
->make_oauth_request(
'access token'
,
request_url
=>
$uri
,
token
=>
$self
->request_token,
token_secret
=>
$self
->request_token_secret,
%$params
,
);
my
$http_req
= HTTP::Request->new(
GET
=>
$uri
);
$http_req
->header(
authorization
=>
$request
->to_authorization_header);
my
$res
=
$self
->user_agent->send_request(
$http_req
);
if
(
$res
->is_success ) {
my
$response
= Net::OAuth->response(
'access token'
)->from_post_body(
$res
->content);
$self
->clear_request_token;
$self
->clear_request_token_secret;
$uri
->query(
$res
->content);
my
%res_params
=
$uri
->query_form;
return
(
$self
->access_token(
$res_params
{oauth_token}),
$self
->access_token_secret(
$res_params
{oauth_token_secret}),
);
}
else
{
croak
sprintf
(
"Something went wrong on GET %s: %s"
,
$uri
,
$res
->status_line )
}
}
sub
authorization_request_token {
my
(
$self
,
%params
) =
@_
;
my
$uri
=
$self
->request_token_url;
my
$request
=
$self
->make_oauth_request(
'request token'
,
request_url
=>
$uri
,
%params
,
);
my
$msg
= HTTP::Request->new(
GET
=>
$uri
);
$msg
->header(
authorization
=>
$request
->to_authorization_header);
my
$res
=
$self
->user_agent->send_request(
$msg
);
if
(
$res
->is_success ) {
$uri
->query(
$res
->content);
my
%res_param
=
$uri
->query_form;
$self
->request_token(
$res_param
{oauth_token});
$self
->request_token_secret(
$res_param
{oauth_token_secret});
}
else
{
croak
sprintf
(
"Something went wrong on GET %s: %s"
,
$uri
,
$res
->status_line )
}
}
1;