Skip to content

Instantly share code, notes, and snippets.

@sh4869
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh4869/9562247 to your computer and use it in GitHub Desktop.
Save sh4869/9562247 to your computer and use it in GitHub Desktop.
TwiiterのOauth認証とkeyの保存についてのプログラム
CONSUMER_KEY = YOUR_CONSUMER_KEY
CONSUMER_SECRET = YOUR_CONSUMER_SECRET
# Coding: UTF-8
#このプログラムはhttps://github.com/yukkurisinai/random_kmb_icon を参考にさせていただきました。
require 'oauth'
require 'oauth/consumer'
require 'twitter'
require './key.rb'
SourcePath = File.expand_path('../', __FILE__)
TokenFile = "#{SourcePath}/token"
def oauth_first
@consumer = OAuth::Consumer.new(CONSUMER_KEY ,CONSUMER_SECRET,{
:site=>"https://api.twitter.com"
})
@request_token = @consumer.get_request_token
puts "Please access this URL: #{@request_token.authorize_url}"
puts "and get the Pin code."
print "Enter your Pin code:"
pin = gets.chomp
@access_token = @request_token.get_access_token(:oauth_verifier => pin)
open(TokenFile, "a" ){|f| f.write("#{@access_token.token}\n")}
open(TokenFile, "a" ){|f| f.write("#{@access_token.secret}\n")}
end
unless File::exist?(TokenFile)
oauth_first
end
@rest_client = Twitter::REST::Client.new do |config|
open(TokenFile){ |file|
ACCESS_TOKEN = file.readlines.values_at(0)[0].gsub("\n","")
}
open(TokenFile){ |file|
ACCESS_SECRET = file.readlines.values_at(1)[0].gsub("\n","")
}
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = ACCESS_TOKEN
config.access_token_secret = ACCESS_SECRET
end
time = Time.now
@rest_client.update("Hello World (#{time})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment