Created
February 15, 2012 20:44
-
-
Save tatat/1838880 to your computer and use it in GitHub Desktop.
Twitterで OAuth + SSL + まるちぱぁと してみた時の
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mime/types' | |
require 'net/https' | |
require 'net/http/post/multipart' # sudo gem install multipart-post | |
require 'oauth' # sudo gem install oauth | |
CONSUMER_KEY = '' | |
CONSUMER_SECRET = '' | |
ACCESS_TOKEN = '' | |
ACCESS_TOKEN_SECRET = '' | |
MEDIA_API_HOST = 'https://upload.twitter.com' | |
MEDIA_API_PATH = '/1/statuses/update_with_media.json' | |
pathname = ARGV[0] # 画像ファイルへのパス | |
status = ARGV[1] # 本文 | |
if !pathname || !status | |
puts sprintf('Error: ruby %s <image> <status>', __FILE__) | |
exit 1 | |
end | |
consumer = OAuth::Consumer.new( | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
:site => MEDIA_API_HOST | |
) | |
access_token = OAuth::AccessToken.new( | |
consumer, | |
ACCESS_TOKEN, | |
ACCESS_TOKEN_SECRET | |
) | |
File.open(pathname, 'rb') do |image| | |
url = URI.parse(MEDIA_API_HOST + MEDIA_API_PATH) | |
filename = File.basename pathname | |
mimetype = MIME::Types.of(filename)[0] | |
request = Net::HTTP::Post::Multipart.new( | |
url.path, | |
'status' => [status].pack('a*'), | |
'media[]' => UploadIO.new(image, mimetype) | |
) | |
access_token.sign! request | |
https = Net::HTTP.new(url.host, url.port) | |
https.use_ssl = true | |
https.verify_mode = OpenSSL::SSL::VERIFY_NONE # ここちゃんと設定したほういいかも | |
response = https.start do |http| | |
http.request request | |
end | |
puts response.body | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment