尝试在 Sinatra 中通过 POST 将图像上传到 Twitter 时出现问题
我在 Ruby 1.8.7 中使用 Sinatra 1.2.6 并且我有一个类似于 Twitter 客户端的东西我正在写作。我正在使用 John Nunemaker 编写的 Twitter gem 版本 1.7.2。对于数据库 ORM,我使用 Sequel 3.29.0。
总体而言,一切进展顺利。我有一个良好的 Oauth 序列,任何经历 Oauth 流程的用户都可以将推文发布到我的应用程序。
然而,我一生都无法使用 update_with_media 进行媒体上传。我正在尝试上传一个多部分八位字节流图像文件,将其保存在内存中,然后将其提供给 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
然而,当我尝试这个时,我得到:
Twitter::Unauthorized - POST https://upload.twitter.com/1/statuses/update_with_media.json: 401: Could not authenticate with OAuth.
它说导致此错误的行是 client.update_with_media 行。
我正在尝试使用 Rack::RawUpload,但我不知道我是否'我错误地使用了它。如果我不需要使用它,我就不会,但我目前陷入困境。在此代码片段之外唯一使用它的是我的代码顶部的内容:
require 'rack/raw_upload'
use Rack::RawUpload
对此的任何帮助将不胜感激。我也尝试过使用 Tempfile.new() ,但这似乎没有多大帮助,而且我要么收到 401 要么 403 错误。我对 Ruby 相当陌生,因此尽可能明确地了解所需的更改将会非常有帮助。
我应该注意,如果可能的话,我想避免将文件放在文件系统上。我实际上只是在这里传递上传内容,之后在我的场景中我永远不需要访问磁盘上的文件。将文件保存在内存中是更可取的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要检查您的库 HTTP 标头的设置方式以及如何逻辑连接到您在此处编写的 POST 方法。问题是,对于 upload_with_media,此 gem 版本中的 twitter api 要求您使用 http://upload.twitter.com 上传端点而不是默认的 api 端点。
gem 可能会强制 api 站点,因此虽然基于 OAuth 的状态更新工作正常,但当您尝试使用图像时它会崩溃。您需要检查 gem 文档以了解如何强制上传 twitter 站点到此方法的 HTTP 标头中。
或者,考虑更新到最新的 Twitter gem。这是我从 http://rdoc.info/gems/twitter 得到的
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