Created
August 3, 2015 03:44
-
-
Save fangyuan/bec5f1625d987f89851a to your computer and use it in GitHub Desktop.
Sina Weibo OpenAPI written in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WWW::Sina::Weibo::OAuth; | |
use utf8; | |
use Moose::Role; | |
use DateTime; | |
sub authorize_url { | |
my $self = shift; | |
my $params = { | |
client_id => $self->appkey, | |
response_type => 'code', | |
redirect_uri => $self->redirect_uri, | |
}; | |
my $url = sprintf( 'https://api.weibo.com/oauth2/authorize?%s', join( '&', map { sprintf( '%s=%s', $_, $params->{$_} ) } keys %$params ) ); | |
return $url; | |
} | |
sub get_access_token { | |
my $self = shift; | |
my $authorize_code = shift; | |
return unless $authorize_code; | |
my $params = { | |
client_id => $self->appkey, | |
client_secret => $self->appsecret, | |
grant_type => 'authorization_code', | |
code => $authorize_code, | |
redirect_uri => $self->redirect_uri, | |
}; | |
my $now = DateTime->now( time_zone => 'Asia/Shanghai' ); | |
my $res = $self->ua->post( 'https://api.weibo.com/oauth2/access_token' => form => $params )->res->json; | |
return unless $res and $res->{expires_in}; | |
$res->{expired_at} = $now->clone->add( seconds => $res->{expires_in} ); | |
$res->{refresh_at} = $now->clone->add( seconds => $res->{remind_in} ); | |
return $res; | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WWW::Sina::Weibo::Request; | |
use Moose::Role; | |
use JSON::XS qw/decode_json/; | |
use Data::Dumper; | |
sub url { | |
my $self = shift; | |
my $base_url = shift; | |
my $params = shift; | |
return $base_url . '?' . join( '&', map { $_ . '=' . $params->{$_}; } keys %$params ); | |
} | |
sub get { | |
my $self = shift; | |
my $url = shift; | |
my $response = $self->ua->get($url); | |
if ( $response->is_success ) { | |
my $data; | |
eval { $data = decode_json( $response->content ); }; | |
return $data; | |
} | |
print Dumper($response); | |
return; | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WWW::Sina::Weibo::User; | |
use utf8; | |
use Moose::Role; | |
sub user_show { | |
my $self = shift; | |
my $args = shift; | |
my $params = { | |
access_token => $args->{access_token} || $args->{token}, | |
source => $self->appkey, | |
}; | |
map { $params->{$_} = $args->{$_} } grep { defined $args->{$_} } qw/uid screen_name/; | |
return unless $params->{access_token}; | |
return unless $params->{uid} || $params->{screen_name}; | |
my $url = 'https://api.weibo.com/2/users/show.json'; | |
my $res = $self->ua->get( $url => form => $params )->res->json; | |
return $res; | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WWW::Sina::Weibo; | |
use utf8; | |
use Moose; | |
use MooseX::Singleton; | |
use DateTime; | |
use Mojo::UserAgent; | |
use Data::Dumper; | |
our $VERSION = '0.01'; | |
with 'WWW::Sina::Weibo::OAuth', 'WWW::Sina::Weibo::User'; | |
has appkey => ( is => 'ro', isa => 'Str', required => 1 ); | |
has appsecret => ( is => 'ro', isa => 'Str', required => 1 ); | |
has redirect_uri => ( is => 'ro', isa => 'Str', default => '' ); | |
has ua => ( is => 'ro', isa => 'Mojo::UserAgent', lazy => 1, default => sub { Mojo::UserAgent->new } ); | |
around BUILDARGS => sub { | |
my $orig = shift; | |
my $class = shift; | |
if ( @_ == 0 ) { | |
my $config = {}; | |
return $class->$orig( | |
appkey => $config->{appkey}, | |
appsecret => $config->{appsecret}, | |
redirect_uri => $config->{auth_redirect_uri}, | |
); | |
} | |
else { | |
return $class->$orig(@_); | |
} | |
}; | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment