Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active April 19, 2018 06:39
Show Gist options
  • Save komasaru/bdd2cf8f43a52044c23029b584b44f45 to your computer and use it in GitHub Desktop.
Save komasaru/bdd2cf8f43a52044c23029b584b44f45 to your computer and use it in GitHub Desktop.
Ruby script to tweet with only OAuth.
#! /usr/local/bin/ruby
# coding: utf-8
#=ツイートテスト(OAuth ライブラリのみを使用)
#
# date name version
# 2017.01.16 mk-mode 1.00 新規作成
#
# Copyright(C) 2017 mk-mode.com All Rights Reserved.
#---------------------------------------------------------------------------------
# 引数 : なし
#---------------------------------------------------------------------------------
#
require 'json'
require 'oauth'
class TweetOauth
CONS_KEY = "... Consumer Key ..."
CONS_SEC = "... Consumer Key Secret ..."
ACCS_KEY = "... Access Token ..."
ACCS_SEC = "... Access Token Secret ..."
SITE = "https://api.twitter.com/"
URL = "#{SITE}1.1/statuses/update.json"
def initialize
cons = OAuth::Consumer.new(CONS_KEY, CONS_SEC, site: SITE)
@accs = OAuth::AccessToken.new(cons, ACCS_KEY, ACCS_SEC)
end
def exec
puts text = "これはテストです!"
res = @accs.post(URL, status: text)
puts JSON.parse(res.body)
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each { |tr| $stderr.puts "\t#{tr}" }
exit 1
end
end
TweetOauth.new.exec if__FILE__ == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment