我有一个带有 OAuth API 的 Rails 应用程序。我正在使用 Doorkeeper gem 进行 OAuth 2 身份验证。我的 API 允许发布带有图像文件附件的消息。我想从 Ruby 控制台测试它。现在,问题是 - 如何使用访问 token 签署发布请求?
Doorkeeper wiki 提供了使用 OAuth2 gem 本身测试 API 的教程。问题是 OAuth2 类不提供使用文件附件发布多部分消息的方法(据我所知)。
https://github.com/applicake/doorkeeper/wiki/Testing-your-provider-with-OAuth2-gem
然后还有 multipart-post gem,它允许将文件作为附件发布到 Rails API。但我不知道如何使用 access_token 签署此类请求,以及如何通过 Doorkeeper 身份验证。
https://github.com/nicksieger/multipart-post
那么将多部分消息发布到使用 access_token 签名的 Rails API 的正确方法是什么?
最佳答案
oauth2 gem 似乎不支持分段上传。检查这个问题:https://github.com/intridea/oauth2/issues/81
解决方法是将 access_token
作为查询字符串或 header 包含在您的参数中。按照 README 中的示例:
require 'net/http/post/multipart'
url = URI.parse('http://www.example.com/upload')
File.open("./image.jpg") do |jpg|
req = Net::HTTP::Post::Multipart.new url.path,
"file" => UploadIO.new(jpg, "image/jpeg", "image.jpg")
# here you include the token in headers
req['Authorization'] = "Bearer #{THE_ACCESS_TOKEN}"
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
关于ruby-on-rails-3 - 导轨 : Testing OAuth API by posting multiparted messages with file attachment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10410818/