tags:

views:

293

answers:

1

I registered an application with Twitter. I use Net::Twitter::OAuth to manage the interaction with Twitter.

I managed to redirect the user to allow him to install the application in his Twitter account. The application is installed with read & write access. I have read access, but I didn't manage to send any tweet in his behalf: Twitter returns that the call is no authorized.

I'm using my own Twitter account to test.

Here is the code I use (Perl & Catalyst):

# step 1: Redirect user to Twitter
my $client = Net::Twitter::OAuth->new(
traits          => ['OAuth'],
consumer_key    => Bargain->config->{'consumer_key'},
consumer_secret => Bargain->config->{'consumer_secret'},
);

my $url = $client->oauth->get_authorization_url({ callback => $callback_url});

$c->response->cookies->{oauth} = {
      value => {
          token => $client->request_token,
          token_secret => $client->request_token_secret,
      },
};

$c->response->redirect($url);


# step 2 - After installing the app, Twitter redirects the user here
my $verifier = $c->req->params->{oauth_verifier};
my $oauth_token = $c->req->params->{oauth_token};

$client->request_token($client->request_token);
$client->request_token_secret($client->request_token_secret);

my($access_token, $access_token_secret) = 
    $client->request_access_token(verifier => $verifier);

# step 3 - With all the info, Access suer account
my $nt = Net::Twitter::OAuth->new(
     traits          => ['OAuth'], # 'API::REST', 
     consumer_key    => Bargain->config->{'consumer_key'},
     consumer_secret => Bargain->config->{'consumer_secret'},
);

$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);

if ( $nt->authorized ) {
    print "Authorized, sending tweets\n";
    print $nt->friends_timeline, "\n"; # OK

    $nt->update('First example'); # Does not work
}

Any idea what I am missing, or what I am doing wrong?

+2  A: 

The docs for Net::Twitter::Oauth claim:

This module is deprecated. Use Net::Twitter instead.

It appears as if you should use Net::Twitter::Role::OAuth for modern code.

Chas. Owens
Thnaks, it seems to work. how can I get the user ID? It is required to gets the user status, but I do not see any function to get the id/username
Julien
It turned out I did not have the latest version: 3.04003 instead of 3.04006
Julien