Handling ENV Variables in api request - ruby-on-rails

I am making a request to the facebook graph api and have saved my USER_ID and ACCESS_TOKEN in ENV variables. I'm wondering if this is a best practice as this morning I am encountering a URI error that I was not getting yesterday.
class FacebookFeed
#Constants
VANDALS_ID = ENV['VANDALS_FB_ID']
FB_ACCESS_TOKEN = ENV['FACEBOOK_ACCESS_TOKEN']
FACEBOOK_URL = 'https://graph.facebook.com/"#{VANDALS_ID}"/posts/?access_token="#{FB_ACCESS_TOKEN}"'
def get_feed
uri = URI(FACEBOOK_URL)
response = HTTParty.get(uri)
results = JSON.parse(response.body)['data']
puts results
end
end
So in the Rails console I am just trying to get a response but am getting:
URI::InvalidURIError: bad URI(is not URI?): https://graph.facebook.com/"#{VANDALS_ID}"/posts/?access_token="#{FB_ACCESS_TOKEN}"
This is strange as this was working yesterday. Is there anything I am missing or is there a better way to store my User_ID and Access Token?
Update
When doing a 'puts uri' this is returned:
https://graph.facebook.com/%22%23%7BVANDALS_ID%7D%22/posts/?access_token=%22%23%7BFB_ACCESS_TOKEN%7D%22
I assume this is what is being sent as the GET request, because when I then do 'puts response' I get:
{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}
How do I construct the request correctly?

After talking to Rich on Skype, the problem was resolved with Sergey Kishenin's comment:
FACEBOOK_URL = "https://graph.facebook.com/#{VANDALS_ID}/posts/?access_token=#{FB_ACCESS_TOKEN}"

Related

Rails API 422 Unprocessable Entity: No verification key available, heroku

I created a Rails API with a JWT authentication system and deployed it to Heroku. When I request the endpoints locally, all seems to be working fine but when I make requests to the live endpoints (i.e the Heroku deployed app) I get a: 422 Unprocessable Entity server error and the response body looks like this:
{
"message": "No verification key available"
}
The class responsible for encoding and decoding the auth token is defined as follows:
class JsonWebToken
# secret to encode and decode token
HMAC_SECRET = Rails.application.secrets.secret_key_base
def self.encode(payload, exp = 24.hours.from_now)
# set expiry to 24 hours from the creation time.
payload[:exp] = exp.to_i
# sign token with application secret
JWT.encode(payload, HMAC_SECRET)
end
def self.decode(token)
# get payload, first index in decoded Array
body = JWT.decode(token, HMAC_SECRET)[0]
HashWithIndifferentAccess.new body
# rescue from all decode errors
rescue JWT::DecodeError => e
# raise custom error to be handled by custom handler
raise ExceptionHandler::InvalidToken, e.message
end
end
I have an endpoint /signup where I can make a POST request to register a new user and POST /todos which is accessible and available only to registered users. Making a registration request works perfectly fine, but when I try to make the POST request to the /todos endpoint it raises an error.
The association between user and suit is 1:m respectively.
Please if you have any idea on how I can fix this, I'll be very grateful, thanks : ).
I finally figured a way out by altering the Rails.application.secrets.secret_key_base to Rails.application.secret_key_base. For a more detailed review on this please check out this link. Hopefully, this will help someone facing a similar issue.
This was also my problem. After checking out my json_web_token.rb file, I figured out that I had written the following line:
HMAC_SECRET = Rails.application.secrets.secret_key_base
There is an extra secrets reference, which is causing the problem. It should be:
HMAC_SECRET = Rails.application.secret_key_base
But as far as I'm concerned, you managed to figure it out yourself!

Undefined method `OAuth' when making HTTP request to Twitter api

I'm getting the following OAuth error when trying to make a request to the Twitter streaming api:
"#NoMethodError: undefined method `OAuth' for #TwitterMoment:0x007fa081d821f0"
def query
authorisation_header = OAuth oauth_consumer_key=ENV["oauth_consumer_key"], oauth_nonce=ENV["oauth_nonce"], oauth_signature=ENV["oauth_signature"], oauth_signature_method=ENV["oauth_signature_method"], oauth_timestamp=ENV["oauth_timestamp"], oauth_token=ENV["oauth_token"], oauth_version=ENV["oauth_version"]
response = HTTParty.get("https://stream.twitter.com/1.1/statuses/filter.json?locations=-#{#bounds}", headers: {"Authorization" => authorisation_header})
end
OAuth is included in my gemfile.
Any ideas would be very much appreciated! This is my first Stack Overflow question :)
You're using OAuth here as a function/method, but that method doesn't exist. There's no def OAuth(...) anywhere in the oauth gem, so it explodes and gives you that NoMethodError.
Judging from the Header example at the bottom of this question, I think you've confused the header string for Ruby code.
Instead, you either need to make the string yourself (a bit annoying to do safely), or use the OAuth gem's methods (API) to do so.
Here's an example from the OAuth github repo:
consumer = OAuth::Consumer.new(
options[:consumer_key],
options[:consumer_secret],
:site => "http://query.yahooapis.com"
)
access_token = OAuth::AccessToken.new(consumer)
response = access_token.request(
:get,
"/v1/yql?q=#{OAuth::Helper.escape(query)}&format=json"
)
rsp = JSON.parse(response.body)
pp rsp
This example may work for you (I'm not able to test it locally here, sorry):
def query
consumer = OAuth::Consumer.new(
ENV["oauth_consumer_key"],
ENV["oauth_consumer_token"],
site: "https://stream.twitter.com"
)
access_token = OAuth::AccessToken.new(consumer)
response = access_token.request(
:get,
"/1.1/statuses/filter.json?locations=-#{OAuth::Helper.escape(#bounds)}"
)
response = JSON.parse(response.body)
pp response # Just a bit of debug printing for the moment; remove this later.
response
end
An addendum:
Usually I might have directed you to use an existing Twitter client gem, such as https://github.com/sferik/twitter, but in this case it looks like they haven't implemented the Moments API yet.

Yelp Place API returning "Invalid Signature" Error only from Nginx on EC2

Problem: I am getting an "Invalid Signature" error from Yelp API only from production (running on nginx server in AWS) When I run locally on my localhost:3000, there is no signature error, and everything works fine.
I am using the yelp gem in rails. Here's some code in ruby.:
$client = Yelp::Client.new({
consumer_key: $SL_CONSUMER_KEY,
consumer_secret: $SL_CONSUMER_SECRET,
token: $SL_TOKEN,
token_secret: $SL_TOKEN_SECRET
})
begin
$client.search("Los Angeles")
rescue => error
puts error.message
puts error.inspect
end
error.message prints out: "Signature was invalid"
error.inspect prints out: < Yelp::Error::InvalidSignature: Signature was invalid >
Everything works when I run locally on rails' Webrick server but when I run it in production, I get an "Invalid Signature" error.
Has anyone seen this? I've looked at some relevant posts, but this seems different. Thanks!
This will probably not pertain to most people, but the off chance it could help someone, here it is:
My "time" was effed up on my EC2 instance. So for example, in ruby, Time.now was not printing the actual time. (I think it was off by a few minutes or so).
Anyway, Yelp API requires a oauth_timestamp when you send a request. Of course, then, my request was timing out b/c the time was off.
How did I found this error out?
I just pinged the URL on my browser with the oauth, token, oauth_timestamp, etc. (few more) as query params. The browser spits out the error response in JSON, and it was saying that my request was timing out. When you use the ruby Yelp Client and catch the exception in code, it doesn't spit out the error response in terminal, so it's a bit more difficult to locate the exact root of the error.
How I solved it:
I re-calibrated the time in my ec2 instance by following the directions here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html
Problem is solved. Peace.
Invalid signature error in Yelp API occurs due to two reasons .
First , Either of your four keys i.e consumer_key , consumer_secret_key , Token & Token Secret is invalid . Secondly Parameters passed to Yelp API Function are either invalid or any of those are nil .

Ruby - gem instagram parser error on response

Faraday Parsing Error 757
I'm new with Ruby and i'm trying to do a simple instragram integration using instagram gem.
I already configured tokens (access_token, client_secret, client_id) with Insagram.config()
Now i'm facing an error while trying to get recent media from instagram inside my controller.
The request:
#medias = Instagram.user_recent_media(196583629, {:count => 1})
The error:
Faraday::Error::ParsingError at /contests/385
757: unexpected token at '�'
Console:
faraday_middleware (0.9.1) lib/faraday_middleware/response/parse_json.rb:11:in `block in <class:ParseJson>'
I don't know if might be the response that i'm getting is broken etc..
Is there a way to check how and what's my response ?
Appreciate any help :)
Thanks.
I tried to replicate this, but the userid you gave is protected.
However, the following code works ok for me (using Snoop Doggs ID: 1574083)
require 'instagram'
Instagram.configure do |config|
config.client_id = ENV['INSTA_CLIENT']
config.client_secret = ENV['INSTA_SECRET']
end
#medias = Instagram.user_recent_media(1574083, {:count => 1})
puts #medias.first
However, unexpected token errors basically imply that the JSON given by the server is not valid json, normally because it's not JSON at all or it's invalid (leading " for example...)
If you do a manual curl of that userid, what do you get?
curl https://api.instagram.com/v1/users/196583629/media/recent/?client_id='clientidhere'

Bad URI with Ruby

I am making an Oauth call to the Facebook API to get myself an access_token
def access_token
token_uri = URI("https://graph.facebook.com/oauth/access_token?client_id=#{CLIENT_ID}&client_secret=#{CLIENT_SECRET}&grant_type=client_credentials")
token_response = HTTParty.get(token_uri)
Rails.logger.info(token_response)
return token_response
end
I get a response and a access_token generated, let's say it's
access_token=123456789|abcdefghijk
but when I then try to use this token
def get_feed
fb_access_token = access_token
uri = URI("https://graph.facebook.com/#{VANDALS_ID}/posts/?#{fb_access_token}")
end
I get an error
URI::InvalidURIError: bad URI(is not URI?)
and the uri generated stops at the | even though there are more characters after the pipe to complete my access_key
https://graph.facebook.com/id-here/posts/?access_token=123456789|
How do I get the full access token available in my URI?
The reason you are getting an error is that | symbol is not allowed in the proper URI, hence it needs to be escaped before it's parsed. URI comes with a method to do it for you:
uri = URI(URI.escape "https://graph.facebook.com/#{VANDALS_ID}/posts/?#{fb_access_token}")
uri.to_s #=> https://graph.facebook.com/id-here/posts/?access_token=123456789%7Cabcdefghijk
When the url is requested, the server should automatically decode it, so all should work as expected.

Resources