Minor indentation formating changes in a spec file.
[twitter4r-core.git] / bin / t4r-oauth-access
blob2f373b09415897942ab384d73a639e8cbbecd109
1 #!/usr/bin/env ruby
3 require("irb")
4 require("irb/completion")
5 require("rubygems")
7 begin
8 gem('twitter4r', '>0.3.0')
9 require("twitter")
10 require("twitter/console")
11 require("pp")
12 rescue Gem::LoadError
13 begin
14 gem("mbbx6spp-twitter4r", '>=0.3.1')
15 require("twitter")
16 require("twitter/console")
17 require("pp")
18 rescue Gem::LoadError
19 abort("Error: You must install either twitter4r gem from Rubyforge with version 0.3.1 or greater or the mbbx6spp-twitter4r gem from GitHub's servers with version 0.3.1 or greater (and make sure it is a recent version of the gem).")
20 end
21 end
23 module Twitter
24 class OAuthAccess
25 class << self
26 @@OPTIONS = { :key => "", :secret => "" }
27 def parse_options!
28 OptionParser.new do |opt|
29 opt.banner = "Usage: t4r-oauth-access [environment] [options]"
30 opt.on("--key=[<YOUR KEY>]", 'Use this OAuth consumer key.') { |v| @@OPTIONS[:key] = v }
31 opt.on("--secret=[<YOUR SECRET>]", 'Use this OAuth consumer secret.') { |v| @@OPTIONS[:secret] = v }
32 opt.parse!(ARGV)
33 end
34 end
36 def config_file
37 result = ENV["T4R_CONFIG"]
38 file_name = File.expand_path('twitter.yml')
39 result ||= file_name if File.exists?(file_name)
40 file_name = File.expand_path('twitter.yml', 'config')
41 result ||= file_name if File.exists?(file_name)
42 file_name = File.expand_path('~/.twitter.yml')
43 result ||= file_name if File.exists?(file_name)
44 result
45 end
47 def account
48 ENV["T4R_ENV"] || ENV["MERB_ENV"] || ENV["RAILS_ENV"]
49 end
51 def run()
52 # TODO: fill in here
53 consumer = OAuth::Consumer.new(@@OPTIONS[:key], @@OPTIONS[:secret],
54 :site => "https://twitter.com")
55 rtoken = consumer.get_request_token
56 puts "1. Visit this URL to grant your application authorization:"
57 puts " >> #{rtoken.authorize_url}"
58 puts "2. When you have authorized your application to access your account, Twitter.com will display a PIN to you."
59 print "3. Enter the PIN here: "
60 pin = STDIN.readline.chomp
61 atoken = rtoken.get_access_token(:oauth_verifier => pin)
62 puts "4. Your access token details are:"
63 puts " >> key: #{atoken.token}"
64 puts " >> secret: #{atoken.secret}"
65 resp = atoken.get("/account/verify_credentials.json")
66 data = JSON.parse(resp.body)
67 puts
68 puts "Your account details are:"
69 p data
70 end
71 end
72 end
73 end
75 if __FILE__ == $0
76 @twitter = nil
77 Twitter::OAuthAccess.parse_options!
78 config_file = Twitter::OAuthAccess.config_file
79 account = Twitter::OAuthAccess.account
81 if config_file && account
82 @twitter = Twitter::Client.from_config(config_file, account)
83 puts "Used #{config_file} to create client for #{account} account."
84 puts "Follow instructions below to grant authorization to your application and determine access token details."
85 Twitter::OAuthAccess.run()
86 else
87 abort("Please make sure #{config_file} exists and contains your Twitter credentials (separated by account/environment) and that you specify the account/environment to use, e.g. if you have a 'test' section in your configuration file that you want to use set/export T4R_ENV=test as an environment variable or RAILS_ENV=test or MERB_ENV=test")
88 end
89 end