用Perl理解oAuth

6

我在使用Yammer API(https://www.yammer.com/api_doc.html)时遇到了问题,无法进行简单的API请求。我需要获取https://www.yammer.com/api/v1/groups.xml(分组:一组列表)。

我尝试使用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

但接下来呢?我必须在哪里输入这个数字?如何执行所需的请求?

谢谢。 Roman

1个回答

6
可能在授权步骤之后得到的数字是oauth_verifier字符串,需要将其与REQUEST令牌一起发送,以获取ACCESS令牌。
这是oAuth 1.0a实现的强制性部分(我认为现在使用最普遍的实现方式),因为2.0仍然是草案,并且没有许多库来实现它。
我猜你没有向提供程序发送回调URL,他不知道在授权后将用户重定向到哪里。当提供程序不知道回调URL时,他无法将用户重定向回您(消费者)的应用程序。 在这种情况下,规范说应该在屏幕上打印验证器字符串,以便您(用户)可以手动获取它并将其提供给您(消费者)的应用程序,以构建ACCESS TOKEN请求。
如果您提供回调URL(在第一个REQUEST令牌的请求中),那么您很可能不会获得此数字的屏幕,而是会自动将您(用户)重定向到回调URL。
例如,如果您的回调URL是:http://myapp.com/oauth/callback,则提供程序将重定向用户到具有查询字符串中正确值的回调URL。
重定向:http://myapp.com/oauth/callback?oauth_token=xxxx&oauth_verifier=yyyy 然后,您的应用程序应该获取验证器字符串并将其作为参数添加到ACCESS TOKEN请求中(就像之前使用其他参数一样,如nonce、timestamp、oauth_token等)。
作为对此最后一个请求(包括oauth_verifier字符串)的响应,您应该获得ACCESS TOKEN。
这是关于oauth_verifier字符串及其为什么在协议中引入的良好解释: http://hueniverse.com/2009/04/explaining-the-oauth-session-fixation-attack/

你能告诉我们如何在 Net::OAuth::Simple 中实现这个吗?我尝试使用 $app->verifier('869A') 进行设置,但似乎不正确。而 $app->request_access_token((oauth_verifier => $pin)); 也不起作用。 - cringe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接