Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Understanding oAuth with Perl

by Gangabass (Vicar)
on Mar 09, 2011 at 12:36 UTC ( #892179=perlquestion: print w/replies, xml ) Need Help??
Gangabass has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i have a problem making simple API request to the Yammer ( I need to get (Groups: A list of groups). I'm trying to use Net::OAuth::Simple. Here is my
package Yammer; use strict; use base qw(Net::OAuth::Simple); sub new { my $class = shift; my %tokens = @_; return $class->SUPER::new( tokens => \%tokens, urls => { authorization_url => " +ize", request_token_url => " +t_token", access_token_url => " +_token", }, protocol_version => '1.0a', ); } sub view_restricted_resource { my $self = shift; my $url = shift; return $self->make_restricted_request( $url, 'GET' ); } sub update_restricted_resource { my $self = shift; my $url = shift; my %extra_params = @_; return $self->make_restricted_request($url, 'POST', %extra_params) +; } 1;
And here is my main program:
use Yammer; # Get the tokens from the command line, a config file or wherever my %tokens = ( consumer_key => 'Baj7MciMhmnDTwj6kaOV5g', consumer_secret => 'ejFlGBPtXwGJrxrEnwGvdRyokov1ncN1XxjmIm34M', callback => '', ); my $app = Yammer->new(%tokens); # Check to see we have a consumer key and secret unless ($app->consumer_key && $app->consumer_secret) { die "You must go get a consumer key and secret from App\n"; } # If the app is authorized (i.e has an access token and secret) # Then look at a restricted resourse if ($app->authorized) { my $response = $app->view_restricted_resource; print $response->content."\n"; exit; } # Otherwise the user needs to go get an access token and secret print "Go to " . $app->get_authorization_url( callback => 'https://www' . rand() ) . "\n"; print "Then hit return after\n"; <STDIN>; my ($access_token, $access_token_secret) = $app->request_access_token( +$_);
I'm getting messages like Go to And authorizing application on this URL. After that i see message like: You have successfully authorized the following application: 2GIS_yammer To complete the authorization go back to the 2GIS_yammer application and enter the following code: 869A But what next? Where i must enter this number? How to perform request i need? Thanks. Roman

Replies are listed 'Best First'.
Re: Understanding oAuth with Perl
by Anonymous Monk on Mar 09, 2011 at 13:15 UTC
      Thank you for the reply. Now i have no problems with GET request but can't complete POSTs.
      if ($app->authorized) { my $response = $app->view_restricted_resource('https://www.yammer. +com/api/v1/users.xml' ); print $response->content."\n"; $response = $app->update_restricted_resource("https://www.yammer.c +om/api/v1/messages/", body => "Message body", broadcast => "true" ); print $response->content."\n"; exit; }
      I'll get error "POST on Net::OAuth::ProtectedResourceRequest=HASH(0x5590e74) failed: 302 Found". Can you explain why?
        I got 401 Unauthorized response when I do POST. I have no issue with GET. Did you figure out your issue?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://892179]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-07-22 03:08 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (336 votes). Check out past polls.