Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichiban/1075327 to your computer and use it in GitHub Desktop.
Save ichiban/1075327 to your computer and use it in GitHub Desktop.
How to post multipart/form-data with OAuth in Ruby
require 'oauth/consumer'
require 'net/http/post/multipart'
OAuth::VERSION = 1.0
consumer = OAuth::Consumer.new('consumer_key', 'consumer_secret',
:site => 'http://provider.example.com',
:request_token_path => '/request_token',
:authorize_path => '/authorize',
:access_token_path => '/access.token')
request_token = consumer.get_request_token(:oauth_callback => 'http://consumer.example.com/callback')
puts request_token.authorize_url # put the url displayed in your browser then authorize it.
access_token = request_token.get_access_token
File.open("./image.jpg") do |image|
req = Net::HTTP::Post::Multipart.new(url.path,
'text' => 'hi, there',
'image' => UploadIO.new(image, "image/jpeg", "image.jpg"))
access_token.sign! req
res = Net::HTTP.start('provider.example.com', 80) do |http|
http.request(req)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment