Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 630
Richlewis Avatar asked Mar 19 '23 17:03

Richlewis


1 Answers

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.

like image 179
BroiSatse Avatar answered Mar 29 '23 06:03

BroiSatse