http://www.perlmonks.org?node_id=892179

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 (https://www.yammer.com/api_doc.html). I need to get https://www.yammer.com/api/v1/groups.xml (Groups: A list of groups). I'm trying to use Net::OAuth::Simple. Here is my Yammer.pm:
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 => "https://www.yammer.com/oauth/author +ize", request_token_url => "https://www.yammer.com/oauth/reques +t_token", access_token_url => "https://www.yammer.com/oauth/access +_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 => 'https://www.yammer.com/oauth/authorize', ); 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 +.yammer.com/oauth/authorize?rand=' . 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 https://www.yammer.com/oauth/authorize?oauth_token=2sxBkKW1F1iebF2TT5Y7g&callback=https%3A%2F%2Fwww.yammer.com%2Foauth%2Fauthorize%3Frand%3D0.0045166015625 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?