Only getting response codes, but not actual response, from OAuth::AccessToken - ruby

In my User model, I have the following code:
def twitter_client
OAuth::AccessToken.new(twitter_oauth, self.access_token_key, self.access_token_secret)
end
def twitter_oauth
OAuth::Consumer.new(Twitter::Login.consumer_key, Twitter::Login.secret, :site => Twitter::Login.site)
end
This way I can call User#twitter_client like so:
current_user.twitter_client.post('/statuses/update.xml', {'status' => 'foooo', 'Accept' => 'application/xml'})
This works well, and actually does update the status, and returns the following:
#<Net::HTTPOK 200 OK readbody=true>
This really is not a problem for updating statuses. However, when I want to get the latest tweets, all I get is a response code object, without the actual content from the response:
current_user.twitter_client.get('/statuses/user_timeline.xml', {'Accept' => 'application/xml'})
=> #<Net::HTTPOK 200 OK readbody=true>
This returned object is just an instance of Net::HTTPOK and contains no tweet data.
How do I get the tweet data?

How about...?
res = current_user.twitter_client.get('/statuses/user_timeline.xml', {'Accept' => 'application/xml'})
puts res.body

Related

HTTParty post method doesn't return status code when adding follow_redirects = false along with other headers and cookies?

The HTTParty methods are like HTTParty.XXX(urlGoesHere, and Args Here)
I'm doing the following:
params = {:UserName => "uname", :Password => "pwd"}
cookie_hash = HTTParty::CookieHash.new
cookie_hash.add_cookies("key1=val1")
cookie_hash.add_cookies("key2=val2")
options = {
:body=>params,
:headers => { 'Cookie' => cookie_hash.to_cookie_string,
'Accept' => something },
:follow_redirects => false
}
URLUserNamePwd = HTTParty.post(myURL, options) # Is this the right way to do?
When I check the http status code, body I get nothing. When I check in the browser development console, I see 302 redirect, and in response headers I see lot of header pairs returned.
What is the URL you're attempting to hit? 302 Found will be paired with a Location header containing the URL where the resource can be found.

In ruby with sinatra, How to get I response with get method on rest client?

I use ruby with sinatra and I used rest-client on import for payment.
I got token that string typed through post method on specific url: '... /users/getToken'.
Using this token, I wanna get payments information with get method on this url:
get_url = 'https://api/iamport.kr/payments/'+imp_uid
the detail codes are below,
def get_paymentsdetails(token, imp_uid)
get_url = 'https://api.iamport.kr/payments/'+imp_uid
response = RestClient.get get_url, :data => {}.to_json, :accept => :json, :headers => {'Authorization' => token}
json = JSON.parse(response, :symbolize_names => true)
# json = JSON.parse(response.to_json, {:symbolize_names => true})
return json
end
However, I got 401 unauthorized error on this part of code.
response = RestClient.get get_url, :data => {}.to_json, :accept => :json, :headers => {'Authorization' => token}
After I access get_url with specific imp_uid, I got this page,{"code":-1,"message":"Unauthorized","response":null}
I checked parameter token and imp_uid of get_paymentsdetails function have valid string values,, so How can I access response parameter??
I think that there are some problems on response = RestClient.get get_url.... code.
Thanks.
Method 'get' from the 'RestClient' class return some object with attributes. So response have few values. Which of them do you need? Access to them you can get by their names, its described here.
In your case, after response = RestClient.get get_url... you should have variable response and ability to call response.headers, response.code or response.body.
But im afraid that you have some problems with autorization, which means that imp_uid or token is not correct. Thats why remote server sended to you responce with http-code 401 (Unauthorized). If it is so you should try to check your imp_uid and token. If everything is correct try to reach support of iamport.kr .

Not sure why I keep getting 404 error with Ruby Rest Client

I'm using the following code:
RestClient.get "https://myurl.com/apps/v1/company/apps/appname/device/#{device_uuid}", :params => {:client_id => client_id, :client_secret => client_secret} do |response, request, result, &block|
if [404].include? response.code
puts 'ERROR' + response.body
else
response.return!(request, result, &block)
end
end
I am trying to use client_id and client_secret as query string parameters, and I know that when I manually do a get on this url in my browser that it is valid - however when I try to use this rest client get request, I only seem to be getting a 404 resource not found back.
The end result I am trying to do is to get the JSON back from this get request as well - it may need to be a separate question but I am also having issues with getting the JSON contents from the response body.
Thank you for any help
The code from above worked better as:
response = RestClient.get "https://myurl.com/apps/v1/company/apps/app1/devices/#device_uuid}",
{:params => {:client_id => client_id, :client_secret => client_secret}, :accept => :json}
It was not just adding the :accept => :json but also making sure it was passed along as part of my parameters - no idea why it bubbled up as a 404 though.

Api Requests with Ruby gem Typhoeus

What is wrong with the following request?
request = Typhoeus::Request.new("http://fluidsurveys.com/api/v2/groups",
method: :get,
userpwd: "test_user:test_password",
headers: { 'ContentType' => "application/json"})
response = request.body
puts response
This returns undefined method body for #<Typhoeus::Request:0x007f8e50d3b1d0> (NoMethodError)
The following request works fine with httparty:
call= "/api/v2/groups/"
auth = {:username => "test_user", :password => "test_password"}
url = HTTParty.get("http://fluidsurveys.com/api/v2/groups",
:basic_auth => auth,
:headers => { 'ContentType' => 'application/json' } )
response = url.body
puts response
EDIT:
I tried this:
response = request.response
puts response.body
with no luck. I receive this : undefined method body for nil:NilClass (NoMethodError)
From https://github.com/typhoeus/typhoeus
You need to do the get before the response body is available.
EDIT: Here is an operable solution. It doesn't use your website, which I couldn't access even manually. But, this returns response code 200 and the response_body. Running this in my debugger showed the complete response, which you could see using "puts response.inspect".
class TyphoeusTry
require 'typhoeus'
request = Typhoeus::Request.new("http://www.google.com",
method: :get,
userpwd: "test_user:test_password",
headers: { ContentType: "application/json"})
response = request.run
puts response.response_body
end
The problem is that you didn't actually execute your request. The following code should work.
request = Typhoeus::Request.new("http://fluidsurveys.com/api/v2/groups",
method: :get,
userpwd: "test_user:test_password",
headers: { 'ContentType' => "application/json"})
request.run
response = request.response
response_code = response.code
response_body = response.body

400 Bad Request Nestful Ruby

I'm trying to use the Pocket API to authorize my application. So I'm using Nestful to send HTTP requests. And everytime I try sending a request I get a 400 Bad Request. The Pocket documentation says that it could be that it's either a missing consumer key or a missing redirect url.
But now I'm looking at the network tab in Chrome and it says that there is a 500 Internal Service Error. What are these things, and how can I fix them?
My code:
require "nestful"
require "sinatra"
require "uri"
get '/' do
params = {
:consumer_key => '******************************',
:redirect_uri => 'http://localhost:4567/callback'
}
response = Nestful.post 'https://www.getpocket.com/v3/oauth/request',
:params => params,
:format => :json
response.body
response.headers
end
get '/callback' do
"hello world"
end
So I got help on my problem. It turns out that params was already a hash, and so I did not need to say :params => params because that would be redundant.
Before
response = Nestful.post 'https://www.getpocket.com/v3/oauth/request',
:params => params,
:format => :json
After
response = Nestful.post 'https://getpocket.com/v3/oauth/request',
params,
:format => :json

Resources