我可以使用ruby中的oauth发送帖子请求吗?

时间:2015-03-10 10:27:03

标签: ruby api post oauth http-post

我可以在ruby中使用oauth发送帖子请求吗?

我有一个使用OAUTH接受GET请求的程序但是如果我发送一个帖子请求它会抛出oauth错误

require 'rubygems'
require 'oauth'
require 'uri'
require 'cgi'
require 'json'

@api_key = "xxxx"
@api_secret = "jjj"
consumer = OAuth::Consumer.new(@api_key, @api_secret)
@auth = OAuth::AccessToken.new(consumer)

def make_request(endpoint,method = "GET",params)
            base_url = 'https://api.xxxx.com/v1/' 

            if method == "GET"
                request_data = CGI.escape(params)
                encoded_url = base_url + endpoint + '?q=' +    request_data
                response = @auth.get(encoded_url)
                JSON.parse response.body
            else     
                puts params
                url = base_url + endpoint 
                response = @auth.post(url,params)
                JSON.parse response.body
            end

end

 registrationData = {
    "webhook_uri" => "http://mydo.com/webhooks-callback-url"
}

puts make_request("webhooks","GET")

上述代码的输出在服务器

中是正确的
{"code"=>"OK", "message"=>"SUCCESS."}

但是如果我使用POST而不是GET代码可以工作,但会抛出一些OAUTH ERROR

require 'rubygems'
require 'oauth'
require 'uri'
require 'cgi'
require 'json'

@api_key = "xxxx"
@api_secret = "jjj"
consumer = OAuth::Consumer.new(@api_key, @api_secret)
@auth = OAuth::AccessToken.new(consumer)

def make_request(endpoint,method = "GET",params)
            base_url = 'https://api.xxxx.com/v1/' 

            if method == "GET"
                request_data = CGI.escape(params)
                encoded_url = base_url + endpoint + '?q=' +    request_data
                response = @auth.get(encoded_url)
                JSON.parse response.body
            else     
                puts params
                url = base_url + endpoint 
                response = @auth.post(url,params)
                JSON.parse response.body
            end

end

 registrationData = {
    "webhook_uri" => "http://mydo.com/webhooks-callback-url"
}

puts make_request("webhooks","POST")

上述代码的输出为

{"code"=>"OAuthError", "message"=>"OAuth Authentication Failed"}

当然是错误的输出

我可以用oauth发帖吗?

0 个答案:

没有答案