本文介绍了使用REST_CLIENT进行请求,相当于curl请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用rest_client做这个curl请求,我不断错误。我如何做?
curl请求:它运行良好,并从yahoo返回访问令牌。
curl -u< consumer_key>:< consumer_secret> -dgrant_type = authorization_code& code =< code>& redirect_uri =< redirect_uri> https://api.login.yahoo.com/oauth2/get_token
我的rest_client要求尝试进行工作是:
#get token
yahoo_url =https://api.login.yahoo。 com / oauth2 / get_token /
response = RestClient :: Request.new(
:method =>:get,
:url => yahoo_url,
:client_id = > $ consumer_id,
:client_secret => $ consumer_secret,
:headers => {:accept =>:json,:content_type =>:json},
:params => {'grant_type'=>'authorization_code','code'=> code,'redirect_uri'=> $ yahoo_redirect_url}
).execute
results = JSON.parse to_str)
输入RESULTS:#{results}
p>
response = RestClient.get'https:// dj0yJmk9Q1RKU2xclient_id:[email protected]/oauth2/get_token',{ :params => {'grant_type'=> 'authorization_code','code'=> code,'redirect_uri'=> $ yahoo_redirect_url}}
任何帮助或建议,甚至更好,可以工作,将不胜感激,谢谢
解决方案
请求必须是HTTP POST而不是HTTP GET,并传递参数:
#POST或PUT与哈希发送参数作为urlencode编码的表单主体
#RestClient.post'http:// example。 com / resource',:param1 => 'one'
所以:
response = RestClient.post'https:// dj0yJmk9Q1RKU2xclient_id:[email protected]/oauth2/get_token',:grant_type => 'authorization_code',:code => code,:redirect_uri => $ yahoo_redirect_url
I am trying to make this curl request using rest_client, and I keep getting it wrong. How can I do it?
curl request: it is working well and returning an access token from yahoo.
curl -u "<consumer_key>:<consumer_secret>" -d "grant_type=authorization_code&code=<code>&redirect_uri=<redirect_uri>" https://api.login.yahoo.com/oauth2/get_token
the rest_client request that I am trying to make work is:
# get token
yahoo_url = "https://api.login.yahoo.com/oauth2/get_token/"
response = RestClient::Request.new(
:method => :get,
:url => yahoo_url,
:client_id => $consumer_id,
:client_secret => $consumer_secret,
:headers => { :accept => :json,:content_type => :json},
:params => {'grant_type' => 'authorization_code', 'code' => code, 'redirect_uri' => $yahoo_redirect_url}
).execute
results = JSON.parse(response.to_str)
puts "RESULTS: #{results}"
or this one:
response = RestClient.get 'https://dj0yJmk9Q1RKU2xclient_id:[email protected]/oauth2/get_token', {:params => {'grant_type' => 'authorization_code', 'code' => code, 'redirect_uri' => $yahoo_redirect_url}}
Any help or suggestions or even a better too that can work will be appreciated, thanks in advance.
解决方案
The request must be a HTTP POST instead of a HTTP GET and pass the parameters as in:
# POST or PUT with a hash sends parameters as a urlencoded form body
# RestClient.post 'http://example.com/resource', :param1 => 'one'
so:
response = RestClient.post 'https://dj0yJmk9Q1RKU2xclient_id:[email protected]/oauth2/get_token', :grant_type => 'authorization_code', :code => code, :redirect_uri => $yahoo_redirect_url
这篇关于使用REST_CLIENT进行请求,相当于curl请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!