Ruby on Rails - OAuth 2 多部分帖子(上传到 Facebook 或 Soundcloud)
我正在开发一个 Rails 应用程序,它使用 OmniAuth 为我的用户收集 Oauth/OAuth2 凭据,然后代表他们发布到这些服务。
创建简单的帖子来更新状态源效果很好。现在我需要上传文件。 Facebook 表示“要发布照片,请发出 POST 请求,并将照片文件附件作为多部分/表单数据。” http://developers.facebook.com/docs/reference/api/photo/< /a>
这就是我想做的:
我在这里实现了该模块: Ruby:如何通过 HTTP 作为 multipart/form-data 发布文件? 获取标头和数据...
if appearance.post.post_attachment_content_type.to_s.include?('image')
fbpost = "https://graph.facebook.com/me/photos"
data, headers = Multipart::Post.prepare_query("title" => appearance.post.post_attachment_file_name , "document" => File.read(appearance.post.post_attachment.path))
paramsarray = {:source=>data, :message=> appearance.post.content}
response = access_token.request(:post, fbpost, paramsarray, headers)
appearance.result = response
appearance.save
end
我但是我得到了 OAuth2:: HTTPError - HTTP 400 错误
任何帮助都会令人难以置信...据我所知,将文件上传到 SoundCloud 也需要此信息。
谢谢,
马克
I am working on a Rails App that Uses OmniAuth to gather Oauth/OAuth2 credentials for my users and then posts out to those services on their behalf.
Creating simple posts to update status feeds work great.. Now I am to the point of needing to upload files. Facebook says "To publish a photo, issue a POST request with the photo file attachment as multipart/form-data." http://developers.facebook.com/docs/reference/api/photo/
So that is what I am trying to do:
I have implemented the module here: Ruby: How to post a file via HTTP as multipart/form-data? to get the headers and data...
if appearance.post.post_attachment_content_type.to_s.include?('image')
fbpost = "https://graph.facebook.com/me/photos"
data, headers = Multipart::Post.prepare_query("title" => appearance.post.post_attachment_file_name , "document" => File.read(appearance.post.post_attachment.path))
paramsarray = {:source=>data, :message=> appearance.post.content}
response = access_token.request(:post, fbpost, paramsarray, headers)
appearance.result = response
appearance.save
end
I but I am getting a OAuth2::HTTPError - HTTP 400 Error
Any assistance would be Incredible... As I see this information will also be needed for uploading files to SoundCloud also.
Thanks,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己也曾为此苦苦挣扎。 oauth2 库由 Faraday 支持其 HTTP 交互。只需一点配置,它就支持开箱即用的上传文件。第一步是在构建连接时添加适当的 Faraday 中间件。我的代码示例:
这为法拉第连接添加了多部分编码支持。接下来,当对访问令牌对象发出请求时,您要使用 Faraday::UploadIO 对象。所以:
在上面的代码中:
io - 您要上传的文件的 IO 对象。可以是 File 对象,甚至是 StringIO。
mime_type - 您上传的文件的 mime 类型。您可以尝试检测此服务器端,或者如果用户将文件上传给您,您应该能够从他们的请求中提取 mime 类型。
文件名 - 正在调用您正在上传的文件的名称。这也可以由您自己的选择来确定,或者您可以只使用上传文件的用户所调用的任何名称。
some/url - 将其替换为您要发布到的 URL
{url: 'params'} - 将其替换为您要提供的任何 URL 参数
{ file: upload} - 将其替换为您的多部分表单数据。显然,一个(或多个)键/值对应该有一个文件上传的实例。
Struggled with this myself. The oauth2 library is backed by Faraday for it's HTTP interaction. with a little configuration it supports uploaded files out of the box. First step is to add the appropriate Faraday middleware when building your connection. An example from my code:
This adds the multipart encoding support to the Faraday connection. Next when making the request on your access token object you want to use a Faraday::UploadIO object. So:
In the above code:
io - An IO object for the file you want to upload. Can be a File object or even a StringIO.
mime_type - The mime type of the file you are uploading. You can either try to detect this server-side or if a user uploaded the file to you, you should be able to extract the mime type from their request.
filename - What are are calling the file you are uploading. This can also be determined by your own choosing or you can just use whatever the user uploading the file calls it.
some/url - Replace this with the URL you want to post to
{url: 'params'} - Replace this with any URL params you want to provide
{file: upload} - Replace this with your multipart form data. Obviously one (or more) of the key/value pairs should have an instance of your file upload.
我实际上成功地使用此代码在 Facebook 页面上上传照片:
I'm actually using successfully this code to upload a photo on a fb page :