Net-SinaWeibo

 view release on metacpan or  search on metacpan

lib/Net/SinaWeibo.pm  view on Meta::CPAN


sub app_secret { shift->consumer_secret }

sub _build_api_proxy_sub {
    my ($api) = @_;
    return sub {
         my $self = shift;
         $self->last_api($api->{uri});
         my %params = @_;
         my $uri;
         if (exists $params{id}) {
            $uri = $api->{uri}.'/'.(delete $params{id});
         }
         else {
            $uri = $api->{uri};
         }
         if ($api->{multi_part}) {
             $params{'@'.$api->{multi_part}} = delete $params{ $api->{multi_part} };
         }
         $self->make_restricted_request($uri,$api->{method},%params);
    };
}

sub status_url {
    my ($self,$user_id,$status_id) = @_;

    return $self->{site}.'/'.$user_id.'/statuses/'.$status_id;
}
# auto compile api proxy method
sub import {
    shift;
    no strict 'refs';
    foreach my $k (keys %SINA_API) {
        *{__PACKAGE__.'::'.$k } = _build_api_proxy_sub($SINA_API{$k});
    }
}
1;


=pod

=head1 NAME

Net::SinaWeibo - A simple and lightweight OAuth api for SinaWeibo

=head1 VERSION

version 0.003

=head1 SYNOPSIS

    # from sinaweibo app setting
    my $app_key = 'xxxx';
    my $app_key_secret = 'xxxxxxxxx';
    my $client = Net::SinaWeibo->new(
        app_key => $app_key,
        app_key_secret => $app_key_secret);
    # authorization
    my $callback_url = 'http://youdomain.com/app_callback';
    my $url = $client->get_authorize_url(callback_url => $callback_url);
    # or don't use callback_url,just like a desktop client.
    my $url = $client->get_authorize_url;
    # now $client hold the request_token, but you must authorize your app first.
    # let user go to visit the authorize url.
    say 'Please goto this url:',$url;

    # save these tokens to your file.
    Net::SinaWeibo->save_tokens('~/app/var/tokens/my.tokens',
        app_key => $app_key,
        app_key_secret => $app_key_secret,
        _request_token => $client->request_token->token,
        _request_token_secret => $client->request_token->secret,
        );

    # later,you can load tokens
    my %tokens = Net::SinaWeibo->load_tokens '~/app/var/tokens/my.tokens';

    # After user authorized,you can request access_token with the request token
    my $client = Net::SinaWeibo->new(
        app_key => $tokens{app_key},
        app_secret => $tokens{app_secret},
        tokens => {
            request_token => $tokens{_request_token},
            request_token_secret => $tokens{_request_token_secret},
        }
    );
    my $verifier = '5123876';
    my ($access_token,$access_token_secret) = $client->get_access_token(
        verifier => $verifier,
        );

    # now you can retrieve any restricted resources.
    my $friends = $client->friends;
    # any api can pass any specific parameters
    my $latest_mentions = $client->mentions since_id => 25892384,count => 10,page => 1;
    # upload also support.
    my $ok = $client->upload(status => 'Hello,this first image file', pic => 'images/demo.jpg');
    # profile image
    my $ok = update_profile_image(image => 'images/my_avatar.jpg');
    # enjoy!

=head1 DESCRIPTION

This is a lite OAuth client for SinaWeibo(http://t.sina.com.cn/).

=encoding utf-8

=head1 METHODS

=head2 new(params)

    my $client = Net::SinaWeibo->new(
        app_key => 'sinaweibo_app_key',
        app_secret => 'sina_weibo_app_secret',
        # optional,you can pass access_token/request_token
        tokens => {
            access_token => 'xxxxxx',
            access_token_secret => 'xxxxxxxx',
            # or
            request_token => 'xxxxxxx',
            request_token_secret => 'xxxxx',



( run in 2.656 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )