2011-03-09 54 views
6

我有一個向Yammer(https://www.yammer.com/api_doc.html)發出簡單API請求的問題。我需要得到https://www.yammer.com/api/v1/groups.xml(組:組列表)。用Perl瞭解oAuth

我想使用Net :: OAuth :: Simple。這裏是我的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/authorize", 
      request_token_url => "https://www.yammer.com/oauth/request_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; 

這裏是我的主要程序:

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($_); 

我越來越喜歡

轉到消息 https://www.yammer.com/oauth/authorize?oauth_token=2sxBkKW1F1iebF2TT5Y7g&callback=https%3A%2F%2Fwww.yammer.com%2Foauth%2Fauthorize%3Frand%3D0.0045166015625

並在此URL上授權應用程序。從那以後,我看到這樣的消息:

您已成功授權 以下應用:2GIS_yammer

完成授權回去 到2GIS_yammer應用和 輸入以下代碼:

869A

但接下來呢?我必須在哪裏輸入這個號碼?如何執行我需要的請求?

謝謝。 羅馬

回答

6

可能是你授權步驟後得到數字是oauth_verifier字符串必須以獲得訪問令牌與請求令牌一起發送。

這是oAuth 1.0a實現的強制性部分(我認爲這是現在最常用的實現,因爲2.0仍然是一個草案,並且沒有太多的庫實現它)。

我想你不會發送回調URL給提供者,他不知道授權後重定向用戶。當提供者不知道回調URL時,他不能將用戶重定向回您的(消費者)應用程序。 在這種情況下,規範說它應該在屏幕上打印驗證字符串,以便您(用戶)可以手動將其提交給您的(消費者)應用程序,從而構建對ACCESS TOKEN的請求。

如果您提供回調URL(在第一次請求REQUEST令牌時),那麼很可能您將不會獲得具有此號碼的屏幕,而是您(用戶)會將其重定向到回調URL自動。

E.g.如果您的回調網址是:http://myapp.com/oauth/callback,那麼提供商會將用戶重定向到您的回調網址,並在查詢字符串中顯示正確的值。

重定向:http://myapp.com/oauth/callback?oauth_token=xxxx&oauth_verifier=yyyy

那麼你的應用程序應該驗證字符串,並將其添加爲參數爲訪問令牌的請求(如你與其他參數,如現時,時間戳的oauth_token等以前做)

作爲對最後一個請求的迴應(包含oauth_verifier字符串),您應該獲得ACCESS TOKEN。

這裏是關於oauth_verifier串了很好的解釋,爲什麼它在協議中引入: http://hueniverse.com/2009/04/explaining-the-oauth-session-fixation-attack/

+0

你能告訴我們如何爲'網:: OAuth的:: Simple'做到這一點?我試圖用'$ app-> verifier('869A')'來設置它,但那似乎是錯誤的。 '$ app-> request_access_token((oauth_verifier => $ pin));'也不起作用。 – cringe 2011-10-30 12:51:35