gpt4 book ai didi

ruby-on-rails - 雅虎 OAuth 1.0 回调问题?

转载 作者:行者123 更新时间:2023-12-04 07:40:54 27 4
gpt4 key购买 nike

我想在我的 Web 应用程序中使用 Yahoo Fantasy Sport API,为此我使用 OAuth 进行 Yahoo 登录。当我运行以下代码时,我有消费者 key 和 secret key ,并且成功传递了 key 。它重定向到雅虎登录,它请求访问用户凭据的权限。如果我同意页面重定向到 https://api.login.yahoo.com/oauth/v2/request_auth并显示验证代码。如果我按下验证码页面中的关闭按钮,则不会回调到我的 URL。

 @ts=Time.now.to_i
@callback_url = "http://localhost:3000/callback"
@nonce = SecureRandom.hex()

consumer = OAuth::Consumer.new("my consumerkey","secret key",
{ :site => 'https://api.login.yahoo.com',
:http_method => :post,
:scheme => :header,
:oauth_nonce => @nonce,
:request_token_path => '/oauth/v2/get_request_token',
:authorize_path => '/oauth/v2/request_auth',
:access_token_path => '/oauth/v2/get_token',
:oauth_callback => "http://localhost:3000/callback",
:oauth_timestamp => Time.now.to_i,
:oauth_signature_method => "HMAC-SHA-1",
:oauth_version => "1.0",
:oauth_callback_confirmed => true,
})

request_token = consumer.get_request_token
session[:request_token]=request_token
redirect_to request_token.authorize_url
access_token=request_token.get_access_token
access = ActiveSupport::JSON.decode(access_token.to_json)
if !(access.present?)
@response = "Response failed"
else
@response = access
end

你能告诉我为了获得access_token的回调需要做哪些改变吗?

最佳答案

我认为您在收到回调时感到困惑。如下更改您的代码,您一定会获得访问 token 来进行 Yahoo API 调用。

        @@access_token = nil
@@request_token = nil
def get_request_token
@@consumer = OAuth::Consumer.new('consumer key',
'secret key',
{
:site => 'https://api.login.yahoo.com',
:scheme => :query_string,
:http_method => :get,
:request_token_path => '/oauth/v2/get_request_token',
:access_token_path => '/oauth/v2/get_token',
:authorize_path => '/oauth/v2/request_auth'
})
@@request_token = @@consumer.get_request_token( { :oauth_callback => 'http://localhost:3000/callback' } )
session[:request_token]=@@request_token
redirect_to @@request_token.authorize_url
#redirect_to @@request_token.authorize_url( { :oauth_callback => 'http://localhost:3000/success' } )

end


def callback
request_token = ActiveSupport::JSON.decode(@@request_token.to_json)

if !(request_token.present?)
$request_token_value = "Response failed"
else
$request_token_value = request_token
end
# access_token = @@request_token.get_access_token({:oauth_verifier=>params[:oauth_verifier],:oauth_token=>params[:oauth_token]})
@@access_token = @@request_token.get_access_token(:oauth_verifier=>params[:oauth_verifier])
access_json = ActiveSupport::JSON.decode(@@access_token.to_json)
puts "****************************"
puts $access_json
puts "****************************"
end

关于ruby-on-rails - 雅虎 OAuth 1.0 回调问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com