-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_mock_test.rb
More file actions
30 lines (26 loc) · 888 Bytes
/
http_mock_test.rb
File metadata and controls
30 lines (26 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require "net/http"
require "uri"
require "json"
require "base64"
require "yaml"
module TwitterBearerTokens
def TwitterBearerTokens.getBearerToken(options)
consumer_key = options[:conkey]
consumer_secret = options[:consec]
uri = URI("https://api.twitter.com/oauth2/token")
data = "grant_type=client_credentials"
cre = Base64.strict_encode64("#{consumer_key}:#{consumer_secret}")
authorization_headers = { "Authorization" => "Basic #{cre}"}
bearer_token = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
response = http.request_post(uri, data, authorization_headers)
if response.code == '200'
token_hash = JSON.parse(response.body)
bearer_token = token_hash["access_token"]
else
raise StandardError, 'There was a problem with the Twitter server'
end
end
bearer_token
end
end