Ruby on Rails - OAuth 2 多部分帖子(上传到 Facebook 或 Soundcloud)

发布于 2024-11-01 19:32:15 字数 1183 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-11-08 19:32:15

我自己也曾为此苦苦挣扎。 oauth2 库由 Faraday 支持其 HTTP 交互。只需一点配置,它就支持开箱即用的上传文件。第一步是在构建连接时添加适当的 Faraday 中间件。我的代码示例:

OAuth2::Client.new client_id, secret, site: site do |stack|
  stack.request :multipart
  stack.request :url_encoded
  stack.adapter  Faraday.default_adapter
end

这为法拉第连接添加了多部分编码支持。接下来,当对访问令牌对象发出请求时,您要使用 Faraday::UploadIO 对象。所以:

upload = Faraday::UploadIO.new io, mime_type, filename
access_token.post('some/url', params: {url: 'params'}, body: {file: upload})

在上面的代码中:

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:

OAuth2::Client.new client_id, secret, site: site do |stack|
  stack.request :multipart
  stack.request :url_encoded
  stack.adapter  Faraday.default_adapter
end

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:

upload = Faraday::UploadIO.new io, mime_type, filename
access_token.post('some/url', params: {url: 'params'}, body: {file: upload})

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.

伤痕我心 2024-11-08 19:32:15

我实际上成功地使用此代码在 Facebook 页面上上传照片:

dir = Dir.pwd.concat("/public/system/posts/images")
fb_url = URI.parse("https://graph.facebook.com/#{@page_id}/photos")
img = File.open("myfile.jpg")
req = Net::HTTP::Post::Multipart.new(
  "#{fb_url.path}?access_token=#{@token}", 
  "source" => UploadIO.new(img, "application/jpg", img.path),
  "message" => "some messsage"
) 
n = Net::HTTP.new(fb_url.host, fb_url.port)
n.use_ssl = true
n.verify_mode = OpenSSL::SSL::VERIFY_NONE
n.start do |http|
  @result = http.request(req)
end

I'm actually using successfully this code to upload a photo on a fb page :

dir = Dir.pwd.concat("/public/system/posts/images")
fb_url = URI.parse("https://graph.facebook.com/#{@page_id}/photos")
img = File.open("myfile.jpg")
req = Net::HTTP::Post::Multipart.new(
  "#{fb_url.path}?access_token=#{@token}", 
  "source" => UploadIO.new(img, "application/jpg", img.path),
  "message" => "some messsage"
) 
n = Net::HTTP.new(fb_url.host, fb_url.port)
n.use_ssl = true
n.verify_mode = OpenSSL::SSL::VERIFY_NONE
n.start do |http|
  @result = http.request(req)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文