ruby-on-rails-3 - 导轨 : Testing OAuth API by posting multiparted messages with file attachment

标签 ruby-on-rails-3 http oauth-2.0 multipart

我有一个带有 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/

相关文章:

asp.net - 如何命名要在 Firefox 上下载的文件?

wordpress - 在 wordpress .htaccess 中将特定页面从 http 重定向到 https

java - 在java中没有得到JSON响应

node.js - 通过 OAuth2 从 Node.js 向 BigQuery REST API 进行身份验证

ruby-on-rails - 预期 #count 已更改为 1,但未获得 block

log4j - 在 tomcat jruby-rack 上使用 log4j 记录 Rails 3 应用程序

ruby-on-rails - 与 has_many 达到二级关联

ruby-on-rails-3 - Rspec 和 Capybara 对象匹配

http - Facebook graph api 使用正确的访问 token 返回 400 个错误请求

oauth-2.0 - 如何使用 MailKit 中的 OAuth 2.0 连接到 Exchange Online?