使用 OAuth-Ruby 和 Tumblr API 获取访问令牌 (Rails 3)
我正在使用 OAuth-Ruby 对 Tumblr 应用程序进行 OAuth 身份验证。我能够编写执行 OAuth 各个步骤的代码,但我无法获取访问令牌或实际发出请求。我可以获取请求密钥,将用户重定向到 Tumblr 以进行身份验证并授予访问权限,并接收经过身份验证的请求密钥。但我不能再进一步了。
我已经注册了我的 Tumblr 应用程序;让我们假设这个问题为我提供了以下密钥:
- OAuth Consumer Key: @oauth_consumer_key
- Secret Key: @secret_key
(我有实际值,但出于明显的原因,我将它们隐藏在这里。)
我正在运行以下代码在用户提交表单时运行的控制器中,该表单将信息存储在 @tumblog 变量中:
#0. provided when registering application
@key = @oauth_consumer_key
@secret = @secret_key
@site = 'http://www.tumblr.com'
@consumer = OAuth::Consumer.new(@key, @secret,
{ :site => @site,
:request_token_path => '/oauth/request_token',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:http_method => :post } )
if @consumer
#1. get a request token
@request_token = @consumer.get_request_token;
session[:request_token] = @request_token
session[:tumblog] = @tumblog
#2. have the user authorize
redirect_to @request_token.authorize_url
else
flash[:error] = "Failed to acquire request token from Tumblr."
render 'new'
end
此代码将我带到 Tumblr 的正确页面,用户在其中授予或拒绝我的应用程序访问用户帐户的权限。假设用户授予访问权限,Tumblr 会重定向回我的应用程序,即我在 Tumblr 注册应用程序时提供的回调。到那时,一切都很顺利。
我的 OAuth 回调在控制器中运行以下代码:
if params[:oauth_token] && params[:oauth_verifier]
@tumblog = session[:tumblog]
@request_token = session[:request_token]
#3. get an access token
@access_token = @request_token.get_access_token
. . . .
end
在步骤 3 中,出现问题。我似乎无法实际获得访问令牌:
@access_token = @request_token.get_access_token
有人可以告诉我需要做什么才能获得访问令牌吗?当我运行该行时,出现 OAuth::Unauthorized 错误。
我真的很感激任何建议。我已经用谷歌搜索并尝试了很多天不同的事情。谢谢!
I am using OAuth-Ruby to do an OAuth authentication with a Tumblr application. I am able to write code that progresses through the various steps of OAuth, but I cannot get an access token or actually make a request. I can get a request key, redirect the user to Tumblr to authenticate and grant access, and receive an authenticated request key. But I can't get any farther than that.
I have registered my Tumblr application; let's assume for this question that it has provided me with the following keys:
- OAuth Consumer Key: @oauth_consumer_key
- Secret Key: @secret_key
(I have actual values, but I am keeping them concealed here for obvious reasons.)
I am running the following code within a controller that runs when the user submits a form, which form stores information in the @tumblog variable:
#0. provided when registering application
@key = @oauth_consumer_key
@secret = @secret_key
@site = 'http://www.tumblr.com'
@consumer = OAuth::Consumer.new(@key, @secret,
{ :site => @site,
:request_token_path => '/oauth/request_token',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:http_method => :post } )
if @consumer
#1. get a request token
@request_token = @consumer.get_request_token;
session[:request_token] = @request_token
session[:tumblog] = @tumblog
#2. have the user authorize
redirect_to @request_token.authorize_url
else
flash[:error] = "Failed to acquire request token from Tumblr."
render 'new'
end
This code gets me to the right page at Tumblr, where the user grants or denies my application access to the user's account. Assuming the user grants access, Tumblr redirects back to my application, to a callback I provided when I registered the application with Tumblr. To that point, everything works beautifully.
My OAuth callback runs the following code in the controller:
if params[:oauth_token] && params[:oauth_verifier]
@tumblog = session[:tumblog]
@request_token = session[:request_token]
#3. get an access token
@access_token = @request_token.get_access_token
. . . .
end
At Step 3, there is a problem. I cannot seem to actually get an access token with the line:
@access_token = @request_token.get_access_token
Can someone tell me what I need to do to get the access token? When I run that line, I get a OAuth::Unauthorized error.
I truly appreciate any advice. I've been Googling and trying different things for multiple days. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 Pelle 的 oauth 插件 并对其进行了一些修改以支持 xauth,如下所示:
i use Pelle's oauth plugin and modified it a little to support xauth like this :