summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthea <ta.kupler@gmail.com>2016-09-02 14:28:26 +0200
committerthea <ta.kupler@gmail.com>2016-09-02 14:28:26 +0200
commit9414f88f743bfe4a74301eb410e4f2d45f2d5ab0 (patch)
treec27462c041b63b176cbb5c0cb65d54faadeaafa3
parentea3fc6ffbb8f90aff28ce56fe4432a8d3ff18caa (diff)
ruby script to generate twitter bearer token
-rw-r--r--config/generate_bearer_token.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/config/generate_bearer_token.rb b/config/generate_bearer_token.rb
new file mode 100644
index 0000000..9d8f517
--- /dev/null
+++ b/config/generate_bearer_token.rb
@@ -0,0 +1,52 @@
+require "net/http"
+require "uri"
+require "json"
+require "base64"
+require "optparse"
+
+options = {}
+
+option_parser = OptionParser.new do |opts|
+ opts.banner = "Create your bearer_token for twitter by including following [options]:"
+
+ opts.on("--key KEY", "consumer_key of your twitter application") do |key|
+ options[:conkey] = key
+ end
+
+ opts.on("--secret SECRET", "consumer_secret of your twitter application") do |secret|
+ options[:consec] = secret
+ end
+
+ opts.on("--file FILE", "file where the bearer_token should be stored to (e.g. config/secrets.yml)") do |file|
+ options[:file] = file
+ end
+
+end
+
+option_parser.parse!
+
+if options[:conkey].nil? || options[:consec].nil? then
+ puts option_parser
+ exit
+else
+ consumer_key = options[:conkey]
+ consumer_secret = options[:consec]
+end
+
+uri = URI("https://api.twitter.com/oauth2/token")
+data = "grant_type=client_credentials"
+cre = Base64.encode64("#{consumer_key}:#{consumer_secret}")
+cre.delete!("\n")
+authorization_headers = { "Authorization" => "Basic #{cre}"}
+
+Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
+ response = http.request_post(uri, data, authorization_headers)
+ token_hash = JSON.parse(response.body)
+ bearer_token = token_hash["access_token"]
+end
+
+if options[:file].nil? || options[:consec].nil? then
+ puts bearer_token
+else
+ # put data into config/secrets.yml
+end