Forked from jkotchoff/generate_twitter_bearer_token.rb
Created
November 23, 2017 13:15
-
-
Save WagnerMatos/052e971f3abb08310fec2624701e1dd2 to your computer and use it in GitHub Desktop.
Example of how to generate and use a Twitter bearer token for the purpose of performing application-only authentication for the Twitter API
This file contains hidden or 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
# Generate and use an oauth2 bearer token for the Twitter API in Ruby | |
# | |
# For Application-Only authentication to the twitter API, a 'bearer token' | |
# is required to authenticate agains their endpoints for rate limiting | |
# purposes. | |
# | |
# This script generates a bearer token by posting to twitter and then it | |
# uses that token to poll their API. | |
# | |
# Note, the base 64 encoded consumer credentials for the bearer token needs | |
# to be stripped of newlines in order for this to work. | |
# | |
# The <consumer_key> and <consumer_secret> can be found by administering | |
# a twitter app at: | |
# http://apps.twitter.com | |
# | |
# For documentation on how to generate this bearer token, refer: | |
# https://dev.twitter.com/oauth/reference/post/oauth2/token | |
require 'rubygems' | |
require 'base64' | |
require 'httparty' | |
require 'json' | |
consumer_key = "<consumer_key>" | |
consumer_secret = "<consumer_secret>" | |
credentials = Base64.encode64("#{consumer_key}:#{consumer_secret}").gsub("\n", '') | |
url = "https://api.twitter.com/oauth2/token" | |
body = "grant_type=client_credentials" | |
headers = { | |
"Authorization" => "Basic #{credentials}", | |
"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8" | |
} | |
r = HTTParty.post(url, body: body, headers: headers) | |
bearer_token = JSON.parse(r.body)['access_token'] | |
puts "Twitter bearer token is: #{bearer_token}" | |
api_auth_header = {"Authorization" => "Bearer #{bearer_token}"} | |
url = "https://api.twitter.com/1.1/search/tweets.json?q=nba" | |
puts HTTParty.get(url, headers: api_auth_header).body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment