Net::Twitter::Manual::MigratingToV1_1(3pm) | User Contributed Perl Documentation | Net::Twitter::Manual::MigratingToV1_1(3pm) |
Net::Twitter::Manual::MigratingToV1_1 - Migrating from Twitter API v1 to v1.1
version 4.01043
use Net::Twitter my $nt = Net::Twitter->new( traits => [qw/API::RESTv1_1/], consumer_key => $consumer_key, consumer_secret => $consumer_secret, access_token => $access_token, access_token_secret => $access_token_secret, );
Net::Twitter prior to version 4.0 used Twitter API version 1. Twitter API v1.1 introduced changes that are not entirely backwards compatible, requiring some changes to calling code.
Net::Twitter attempts to provided backwards compatibility where possible. This document describes the changes required to your existing application code, using Net::Twitter, for use with Twitter's API v1.1.
Wherever you create a Net::Twitter object by calling "new", replace trait "API::REST" with "API::RESTv1_1". For most applications, that's all that is required.
If your code currently uses the "RateLimit" role, you'll need to write some custom code provide equivalent functionality.
# With API v1 my $r = $nt->friends; my @friends = @$r; # With API v1.1 my $r = $nt->friends; my @friends = @{$r->{users}};
So, first, drop "API::Search" from your calls to "new". The "API::Search" trait is incompatible with "API::RESTv1_1".
In v1, Twitter returned a hashref with several keys containing meta data. The actual array of results were contained in the "results" slot:
# With Twitter API v1 my $nt = Net::Twitter->new(traits => [qw/API::REST API::Search/]); my $r = $nt->search('perl hacker'); for my $status ( @{$r->{results} ) { # process each status... }
In v1.1, Twitter returns a hash with 2 slots: "search_metadata" and "statuses".
# With Twitter API v1.1 my $nt = Net::Twitter->new(traits => [qw/API::RESTv1_1/], %oauth_credentials); my $r = $nt->search('perl hacker'); for my $status ( @{$r->{statuses} ) { # process each status... }
Paging through search results works differently in v1.1. In v1, Twitter offered a "page" parameter:
# With Twitter API v1 for ( my $page = 1; $page <= 10; ++$page ) { my $r = $nt->search({ q => $query, page => $page, rpp => 100 }); last unless @{$r->{results}}; # process a page of results... }
In v1.1, use "max_id" and "count" to get paged results:
# With Twitter API v1.1 for ( my %args = ( q => $query, count => 100 ), my $n = 0; $n < 1000; ) { my $r = $nt->search({ %args }); last unless @{$r->{statuses}}; $args{max_id} = $r->{statuses}[-1]{id} - 1; $n += @{$r->{statuses}}; # process a page of results... }
Some Twitter API v1 methods are not available in v1.1:
my $r = $nt->show_relationship({ source_screen_name => $user_a, target_screen_name => $user_b, }); if ( $r->{relationship}{source}{following} ) { # $user_a follows $user_b }
2018-01-18 | perl v5.26.1 |