使用REST_CLIENT发出请求,相当于curl请求

我正在尝试使用rest_client来发出这个curl请求,并且我一直都弄错了。 我该怎么做?

curl请求:它运行良好并从yahoo返回访问令牌。

curl -u ":" -d "grant_type=authorization_code&code=&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(response.to_str) puts "RESULTS: #{results}" 

或者这一个:

 response = RestClient.get 'https://dj0yJmk9Q1RKU2xclient_id:be49966a23db7_client_secretb@api.login.yahoo.com/oauth2/get_token', {:params => {'grant_type' => 'authorization_code', 'code' => code, 'redirect_uri' => $yahoo_redirect_url}} 

任何帮助或建议,甚至更好的可以工作将不胜感激,提前感谢。

请求必须是HTTP POST而不是HTTP GET,并传递参数,如下所示:

 # POST or PUT with a hash sends parameters as a urlencoded form body # RestClient.post 'http://example.com/resource', :param1 => 'one' 

所以:

 response = RestClient.post 'https://dj0yJmk9Q1RKU2xclient_id:be49966a23db7_client_secretb@api.login.yahoo.com/oauth2/get_token', :grant_type => 'authorization_code', :code => code, :redirect_uri => $yahoo_redirect_url