在Rails中使用OAuth :: Unauthorized 401问题获取访问令牌请求失败

时间:2022-06-17 08:17:32

First of all, I am trying to connect my app with an site in China. I am not sure if that matters, it shouldn't since that site fully supports Oauth.

首先,我正在尝试将我的应用程序与中国的网站连接起来。我不确定这是否重要,不应该因为该网站完全支持Oauth。

Here is my code.

这是我的代码。

First step:

  require 'oauth'


   def consumer
     OAuth::Consumer.new("206506xxxx", "574705f63783xxxaa4a0bd35e34390fb", :site =>"http://api.t.sina.com.cn")
    end


 def request_token

    @request_token = consumer.get_request_token
  session[:request_token] = @request_token
    session[:request_token_secret] = @request_token.secret
 redirect_to @request_token.authorize_url(:oauth_callback => 'http://localhost:3000/accesstoken')

 end

It worked well, I was able to go to authorize_url and get an oauth_verifier

它工作得很好,我能够去authorize_url并得到一个oauth_verifier

However, I encountered an issue when I was trying to get the access token.

但是,当我尝试获取访问令牌时遇到了一个问题。

 def access_token
 request_token = OAuth::RequestToken.new(
  consumer,
  session[:request_token],
  session[:request_token_secret]
)


access_token = request_token.get_access_token(:oauth_verifier=>params["oauth_verifier"])


 end 

I also tried

我也试过了

   access_token = request_token.get_access_token

But they both returned 401 error.

但他们都返回401错误。

I cleaned the session so it's probably not due to session expiry

我清理了会话,因此可能不是因为会话到期

Any Ideas?

1 个解决方案

#1


-3  

I solved the problem myself, so instead of using

我自己解决了这个问题,所以没有使用

  session[:request_token] = @request_token

I used

   session[:request_token] = @request_token.token 

which worked.

#1


-3  

I solved the problem myself, so instead of using

我自己解决了这个问题,所以没有使用

  session[:request_token] = @request_token

I used

   session[:request_token] = @request_token.token 

which worked.