![](/img/trans.png)
[英]Programmatically get access_token from OAuth - Using JIRA-Ruby gem
[英]Cannot get access token from Yahoo API using Ruby OAUTH Gem
我已经尝试了数周时间,以便能够从Yahoo获得幻想足球联赛的名册。 我写了以下内容,以便从OAUTH获取访问令牌。
第1步(我之前已经将使用者密钥和使用者密码作为变量输入,我在consumer.new语句中也有3个URL,但是堆栈溢出不会让我在帖子中包含超过1个URL。)
@consumer = OAuth::Consumer.new(consumer_key, consumer_secret":request_token_path => URL ,:access_token_path =>URL, :authorize_path =>URL)
第2步
@request_token = @consumer.get_request_token()
步骤3(这成功地给了我一个URL,并且我可以授予我的Yahoo Fantasy Football帐户权限,然后向用户显示一个代码。我将此代码用作誓言验证者字符串,并将其传递到下面)。
@request_token.authorize_url
sleep(30)
@token = OAuth::Token.new("ucjuyt",@request_token)
@access_token=OAuth::AccessToken.new(@consumer, token=@token, secret=@request_token.secret)
p @access_token.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20fantasysports.draftresults&diagnostics=true")
这将返回以下错误。
#<Net::HTTPBadRequest 400 Bad Request readbody=true>
我确定这与我请求访问令牌的方式有关,但是我无法弄清楚如何使此功能正常工作。 我没有UI,正在尝试在我的Ruby代码中运行所有这些程序,可以吗? 我浏览了Yahoos文档,但并没有帮助我。
尝试这个:
consumer_key= <your consumer key>
consumer_secret = <your consumer secret>
code = <the user code you got after you gave permission>
uri = URI('https://api.login.yahoo.com/oauth2/get_token')
req = Net::HTTP::Post.new(uri.path)
req.basic_auth consumer_key, consumer_secret
req.set_form_data({client_id: consumer_key, client_secret: consumer_secret, redirect_uri: 'oob', code: code, grant_type: 'authorization_code'})
sock = Net::HTTP.new(uri.host, uri.port)
sock.use_ssl = true
res = sock.start {|http| http.request(req) }
puts res.body
access_token = JSON.parse(res.body)['access_token']
refresh_token = JSON.parse(res.body)['refresh_token']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.