forked from damog/www-tumblr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuth.pm
44 lines (30 loc) · 1.24 KB
/
OAuth.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package WWW::Tumblr::Authentication::OAuth;
use strict;
use warnings;
use Carp;
use Moose;
use base 'WWW::Tumblr::Authentication';
extends 'WWW::Tumblr';
has 'authorize_url', is => 'rw', isa => 'Str', lazy => 1, default => sub {
my $self = shift;
croak "The consumer_key and secret_key must be defined!"
unless $self->consumer_key && $self->secret_key;
return $self->oauth->authorize_url();
};
has 'oauth_token', is => 'rw', isa => 'Str';
has 'oauth_verifier', is => 'rw', isa => 'Str';
has 'token', is => 'rw', isa => 'Str', lazy => 1, default => sub{ shift->_get_token() };
has 'token_secret', is => 'rw', isa => 'Str', lazy => 1, default => sub { shift->_get_token('secret') };
has '_access_token', is => 'rw';
sub _get_token {
my $self = shift;
my $type = shift || 'token';
croak "Cannot get OAuth token without 'oauth_token' and 'oauth_verifier' defined!"
unless $self->oauth_token && $self->oauth_verifier;
warn "Session looks empty, _get_token will fall, probably"
unless keys %{ $self->_session };
$self->_access_token( $self->oauth->get_access_token( $self->oauth_token, $self->oauth_verifier ) )
unless $self->_access_token;
return $type eq 'secret' ? $self->_access_token->token_secret : $self->_access_token->token;
}
1;