can't get ruby omniauth-ebay-oauth gem example code working - ruby

I am trying to setup and use omniauth-ebay-oauth (https://github.com/evilmartians/omniauth-ebay-oauth) gem to use eBay rest APIs in my app without success.
I set up the required environment variables and run the example code but get a message saying "Sinatra doesn’t know this ditty.". It does not recognise the '/auth/ebay' route, not sure if I have to declare that route myself nor what to put in it if I do. I'm new to ruby and Sinatra so do apologise if this is just something silly and obvious that I'm missing.
require 'omniauth-ebay-oauth'
use Rack::Session::Cookie
use OmniAuth::Builder do
provider :ebay_oauth, ENV['EBAY_CLIENT_ID'], ENV['EBAY_CLIENT_SECRET'],
callback_url: ENV['EBAY_RU_NAME'], name: 'ebay'
end
get '/' do
redirect '/auth/ebay'
end
get '/auth/ebay/callback' do
"Hello, #{request.env['omniauth.auth'].dig('info', 'name')}"
end
I appreciate any help and insight in getting this working. I've googled everywhere and asking here as my last resort.

I opened an issue on the GitHub repository and the gem creator replied in 3 hours. Totally life saver. I will post the solution here to help others.
It is because of security settings of OmniAuth 2.x.
Add the following line at the top, after requires:
OmniAuth.config.allowed_request_methods += %i[get]
This worked like a charm and I can now move forwards with the project.

Related

Twitter handler (sensu and ruby) troubleshooting

I'm trying to get Sensu twitter-handler working on my environment. The issue is that I'm not getting any errors on screen or logs when I cat a .json event into the twitter-handler and, the tweets are not being shown on the linked account.
Here're are my config files:
https://gist.github.com/Mariano-gon/8648427
https://gist.github.com/Mariano-gon/8648455
https://gist.github.com/Mariano-gon/8648489
This is the output I get:
https://gist.github.com/Mariano-gon/8648480
One important note is that in sensu-api.log the request are being recieved:
https://gist.github.com/Mariano-gon/8673758
So, my question is: is there a way to troubleshoot this issue? Any way to debug the handler.rb?
Thanks!
Finally got it working! Thing was the new 'twitter' gem (in my case v5.6) won't work with the code as it was written. I needed to follow this great answer and this thread too. Was just a matter of sintaxis (as usual).
Thanks!

Facebook Sinatra app won't login

I am trying to develop a Facebook Application using the Ruby Heroku pack provided. I have experimented with this before but just created a new app to develop. I am having an issue where I am unable to log in with the app. I click on a button to log in, but after the redirects, I find that nothing has happened. Once in every 20 or so attempts though, it will log me in correctly.
For those who've never used the Heroku hosting for Facebook, here is the template that gets installed. The only changes I have made were to modularize the app.
The relevant routing is as follows:
get "/auth/facebook" do
session[:access_token] = nil
redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
end
get '/auth/facebook/callback' do
session[:access_token] = authenticator.get_access_token(params[:code])
redirect '/'
end
Has anyone ever experienced this before? If one would like to try themselves, this is the location of the app. I have a similar one running that doesn't exhibit this problem, so I really can't figure it out.
EDIT: I think I found the problem. I had set Unicorn preload_app to false, following this piece of information from New Relic. Setting preload_app to true has since fixed the problem. Unfortunately, I cannot remember why I set it to false in the first place (I have adapted some configuration stuff from a previous app). Maybe someone has better knowledge than myself.

Problems attempting to upload image to Twitter via POST in Sinatra

I'm using Sinatra 1.2.6 in Ruby 1.8.7 and I have something like a Twitter client that I'm writing. I am using the Twitter gem version 1.7.2 written by John Nunemaker. For database ORM I'm using Sequel 3.29.0.
Overall, things are working great. I've got a good Oauth sequence working and any user who goes through the Oauth process can post Tweets to my application.
I cannot however for the life of me get media upload working using update_with_media. I'm trying to upload a multi-part octet-stream image file, keep it in memory and then give it to Twitter.
post '/file_upload' do
user_name = params[:user]
if params[:action] == "FILE-UPLOAD"
unless params[:name].match(/\.jpg|png|jpeg/).nil?
#Assume these 3 lines work, and properly authorize to Twitter
current_user = User[:user_name => user_name, :current_account => "1"]
client = current_user.authorize_to_twitter #Handles the Oauth keys/process
client.update("Text status updates work properly")
#Something incorrect is happening in the next two lines.
#I'm either handling the file upload wrong, or posting wrong to Twitter
datafile = params[:file]
client.update_with_media("File upload from Skype: ", datafile)
return "File uploaded ok"
end
end
end
Yet, when I try this, I'm getting:
Twitter::Unauthorized - POST https://upload.twitter.com/1/statuses/update_with_media.json: 401: Could not authenticate with OAuth.
Its saying the line causing this error is the client.update_with_media line.
I am trying to use Rack::RawUpload, but I don't know if I'm using it incorrectly. If I don't need to use it I won't, but I'm just currently stuck. The only thing outside of this code snippet that's using it is this at the top of my code:
require 'rack/raw_upload'
use Rack::RawUpload
Any help on this would be massively appreciated. I've tried messing around with Tempfile.new() as well, but that didn't seem to help much, and I was either getting 401 or 403 errors. I'm fairly new to Ruby, so being as explicit as possible about changes needed would be really helpful.
I should note that I'd like to avoid putting the file on the filesystem if possible. I'm really just passing along the upload here, and I never need access in my scenario to the file on-disk afterward. Keeping the files in-memory is much preferred.
You need to check how your library HTTP headers are setup and logically connected to the POST method you have written here. The thing is that for upload_with_media, twitter api in this gem version requires you to use http://upload.twitter.com upload endpoint instead of the default api endpoint.
The gem may be forcing the api site so while the OAuth based status update works fine, it crashes when you try it with an image. You will need to check the gem documentation to figure out how to force the upload twitter site into the HTTP headers for this method.
Alternatively, consider updating to the latest twitter gem. This is what I got from http://rdoc.info/gems/twitter
The Twitter::API#update_with_media method no longer uses the custom upload.twitter.com endpoint, so media_endpoint configuration has been removed. Likewise, the Twitter::API#search method no longer uses the custom search.twitter.com endpoint, so search_endpoint configuration has also been removed.

Recieving a 404 HTTPError on a working page in Ruby Script

This is my first time asking a question, please be gentle!
I have a Rails application that handles content for a whole bunch of domains (over 100 so far). Each domain either points to where my app is hosted (Heroku, if you're interested), or the original place it was hosted. Every time a domain is ready, it needs to point to the heroku servers, so that my app can serve content for it.
To check to see if a domain has successfully been changed over from its original location to my application, I'm writing a script that looks for a special hidden tag I included in them. If it finds the tag, then the domain is pointing to my app. If not, it hasn't been changed, which I record.
The problem is that, at least for one domain so far, I'm getting a 404 OpenURI::HTTPError exception for my script. Which is strange, because I can visit the site just fine and I can even get it via curl. Does anyone know why a working site would get an error like this? Here's the important snippet:
require 'rubygems'
require 'open-uri'
require 'hpricot'
...
url = "http://www.#{domainname}.com"
doc = Hpricot(open(url)) #<---- Problem right here.
...
Thanks for all of your help!
Welcome to SO!
Here would be my debugging method:
See if you can replicate in irb with open-uri alone, no Hpricot:
$ irb -rubygems -ropen-uri
>> open('http://www.somedomain.com')
Look in your Heroku log to see if it even touches the server.
Look in your original server's log for the same.
Throw open something like Wireshark to see the HTTP transaction, and see if a 404 is indeed coming back.
Start with that, and come back with your results.

Sinatra - how do I debug it when it's online?

I wrote a teeny tiny Sinatra app that runs fine, locally, but for some reason as soon as I put it online, all I get is 'Internal Server Error'.
How do I get the logging output?
I'm running on Dreamhost with passenger, using the instructions from the Sinatra book.
So I added to more handlers:
get '/hello/:name' do
"Hello, #{params[:name]}!"
end
get '/nokogiri-test/' do
doc = Nokogiri::HTML(open('http://www.google.co.il/search?q='+params[:query]))
res = ''
doc.xpath('//li//h3//a').each do |li|
res+= li.content + '<br />'
end
res
end
The first one works fine, the second throws an error.
I'm not interested in why there's an error. I'm interested in how to get feedback for it and resolve errors in the future.
Ah! The answer comes from here.
Never would have thought to look in there, but I got desperate. The solution is to set the environment to :development:
set :environment, :development
I stuck that into my configuration files and it produced all the error output.
Still doesn't solve my problem if I ever want to discover what's causing an error in a PRODUCTION app...
So how would I solve that?
If you're seeing apache's Internal Server Error message, you should be able to check the apache error log to find out what's going on. I think on dreamhost the log file is stored in /home/your_user_name/logs/yourdomain.com/http/error.log

Resources