我在向Yammer(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;
Run Code Online (Sandbox Code Playgroud)
这是我的主要计划:
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($_);
Run Code Online (Sandbox Code Playgroud)
我收到消息
并在此URL上授权应用程序.之后我看到如下消息:
您已成功授权以下应用程序:2GIS_yammer
要完成授权,请返回2GIS_yammer应用程序并输入以下代码:
869A
但接下来呢?我必须输入这个号码?如何执行我需要的请求?
谢谢.罗马
可能在授权步骤之后获得的数字是oauth_verifier字符串,需要与REQUEST令牌一起发送以获取ACCESS令牌.
这是oAuth 1.0a实现的强制性部分(我认为这是现在使用的最常见的实现,因为2.0仍然是草案,并且没有很多库实现它).
我猜你没有向提供商发送回调URL,他不知道授权后在哪里重定向用户.当提供者不知道回调URL时,他无法将用户重定向回您的(消费者)应用程序.在这种情况下,规范说它应该在屏幕上打印验证器字符串,因此您(用户)可以手动接收它并将其提供给您的(消费者)应用程序,从而构建对ACCESS TOKEN的请求.
如果您提供回调URL(在您第一次请求REQUEST令牌时),那么很可能您不会使用此号码获取屏幕,而是您(用户)将自动重定向到回调URL.
例如,如果您的回调网址是:http://myapp.com/oauth/callback,那么提供商会将用户重定向到您的回调网址,并在查询字符串中使用正确的值.
重定向: 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/