Ruby Oauth 文件上传/分段 POST 请求
我已经看了几天了,但还没有 找到了解决方案。有没有办法使用 OAuth-Ruby 上传文件?
我正在使用一个 REST 系统,该系统通过 oauth 保护其资源。我正在使用 ruby 和 oauth-ruby 构建一个测试工具,以便更轻松地将测试数据上传到系统。但我无法抽出时间将文件上传到资源。
当我发送正常请求时,一切正常,但添加文件作为 参数使签名无效。
示例:
@access_token.post("http://.../imageresource", {:name=>"awesome cat"}, {'Content-Type' => 'multipart/form-data'})
有效,但给了我:
<error>
<message>images/POST: Request has no file data</message>
</error>
我不知道如何将文件添加到帖子中。
对此有什么想法吗?
谢谢,
I've been looking at this for a couple of days now and haven't
found a solution. Is there a way to upload a file using OAuth-Ruby?
I am working with a REST system that protects their resource with oauth. I am building a test tool using ruby and oauth-ruby to make it easier to upload test data to the system. But I can't get around to upload files to the resources.
When I send a normal request, everything works but adding a file as a
parameter makes the signature invalid.
Example:
@access_token.post("http://.../imageresource", {:name=>"awesome cat"}, {'Content-Type' => 'multipart/form-data'})
works but gives me:
<error>
<message>images/POST: Request has no file data</message>
</error>
I am not sure how to add a file to the post.
Any thoughts on this?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这已经很旧了,但我也想这样做,这看起来可以做到诡计。
实际上有一个问题 ruby-how-to-post-a-file-via-http-as-multipart-form-data 有一个示例。
I know this is old but I'm looking to do this too, this looks like it could do the trick.
Actually there's a question ruby-how-to-post-a-file-via-http-as-multipart-form-data that has an example.
这对于
oauth
gem 来说要么是不可能做到的,要么是极其困难的。不管怎样,我不知道有什么方法可以使用那个宝石来做到这一点。只要您有一种方便的方法来构造有效的多部分请求正文,就可以使用我的
signet
gem 轻松完成此操作。这种请求正文的构造超出了 OAuth gem 的范围,但对于大多数 HTTP 客户端来说应该很容易完成。然后,httpadapter
gem 可以将请求转换为signet
可以签名的形式。如果httpadapter
不支持您首选的 HTTP 客户端,请告诉我,我会立即解决该问题。请参阅
fetch_protected_resource
< 上的第二个示例/a> 方法来了解如何完成此操作。This is either impossible to do with the
oauth
gem or exceedingly difficult. Either way, I don't know of any way to do it using that gem.It can be done trivially with my
signet
gem as long as you have a handy way to construct a valid multipart request body. The construction of such a request body is out-of-scope of an OAuth gem, but should be pretty easy to do with most HTTP clients. Thehttpadapter
gem can then translate the request into a form thatsignet
can sign. Let me know if your preferred HTTP client isn't supported byhttpadapter
and I'll get that resolved immediately.See the second example on the
fetch_protected_resource
method to get an idea for how this might be done.