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.
Related
I'm trying to understand why is the following method is blocking my app.
url = 'http://192.168.1.33/assets/my_small_pic.jpg'
image_file = open(url).read
It's working perfectly when I try it in the console. But when I do it from an API method, it blocks my app and after a long while I have the following error:
Net::ReadTimeout (Net::ReadTimeout)
What does my app not like my way of reading the file?
I assume you're using 'open-uri' and the api is a part of the same RoR app where you're sending the request to. In this case your app is just being blocked by the first request while you send a second request to it so the second request gives a timeout. You should see this issue in development only. In production things will be a bit different since static assets to be served by the Nginx or Apache. Additionally Rails 4 is by default is thread safe in production which means it can serve multiple requests at a time. So if you're on Rails4 then in production calls to other apis will work as well. For Rails3 you would have to explicitly specify config.threadsafe!
In general I would recommend you to access resources or make any other API calls from the same app directly. It's more efficient. In your example above you can read the file like this:
File.read(File.join(Rails.root, 'public/assets/my_small_pic.jpg'))
If you still want to send the request then to make it work in development you have to start a new thread similar to this:
Thread.new do
open('http://192.168.1.33/assets/my_small_pic.jpg').read
end
I'm using qqfileupload (http://valums.com/ajax-upload/) to create a single drag & drop image upload interface.
The request is being sent to rails, and my rails console is returning
!! Unexpected error while processing request: invalid %-encoding (����JFIFdd��Ducky��Adobed)
which I assume is rails attempting to read the file.
I set my controller to output
return render :text => params
thinking that I could look at what the server was recieving, but I only get the Unexpected error again, which to me says that Rails is hitting this error before getting to the controller.
The params from the javascript console shows
http://localhost:3000/users?qqfile=me.jpg&first_name=&last_name=
the first and last fields are supposed to be blanks.
I stared following this tutorial http://css-tricks.com/ajax-image-uploading/, which looked very similar to the case I was already using, and it lead to this download script http://valums.com/ajax-upload/, which is almost identical to the one I had before, but somehow is slightly different and doesn't return the error above.
Hopefully this helps somebody out.
I'm trying to do an app request, but my requests do not apear in the notification (the globe icon) and I don't know what to do more.
Just to confirm that i've done everything ok, I have followed the JS SDK Request Dialog example:
http://developers.facebook.com/docs/reference/dialogs/requests/
At first time it seems to work ok, and I have a list of my request, but can't see none in facebook.
What I have done so far:
used the fb_graph gem ( https://github.com/nov/fb_graph )
u = FbGraph::User.me(User.find(2).fb_access_token)
u.app_request!(:message => 'punk message')
After that if I do u.app_requests, I can see a list of requests.
As this seems not to work I after tried the Facebook JS SDK, and the result is the same.
My Question is: What am I doing wrong, or missing something?
Thank you
The more I'm around it, the more I'm favoring the Javascript SDK over server side code. Is there a reason for your app to require to do anything serverside, or can you accomplish the same thing client side? Can you give us an example of the Javascript code you tried that failed?
I use the following code to pass data into a website:
require "net/http"
params = {"message"=>"some message", "to"=>"someone"}
Net::HTTP.post_form(URI.parse("http://example.com/m/send"),params)
When I inspect the web page, the form action is http://example.com/m/send and I can post the data using the site itself without any problem.
I keep getting HTTP 404 and my data is not passed to the database.
When I request the page with GET method, then I get HTTP 405, which is an unauthorized request error. This guarantees that the page exists.
Since the url is valid, what would prevent the data being posted? And how can I fix that?
I could not solve the question using Net/HTTP library solely; however, Mechanize gem as Tin Man suggested in the comments solves the problem and successfully posts the data into the server.
It is also more flexible and easier in terms of following redirection. Hence, if anyone runs into this problem like I did, I recommend them using the Mechanize gem.
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.