Class: EphemeralCalc::GoogleAPI::OAuth
- Inherits:
-
Object
- Object
- EphemeralCalc::GoogleAPI::OAuth
- Defined in:
- lib/ephemeral_calc/google_api/oauth.rb
Constant Summary collapse
- SCOPES =
[ "https://www.googleapis.com/auth/userlocation.beacon.registry", "https://www.googleapis.com/auth/cloud-platform", ]
- REDIRECT_URI =
"urn:ietf:wg:oauth:2.0:oob"- TOKEN_URI =
URI("https://www.googleapis.com/oauth2/v4/token")
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #get_code ⇒ Object
- #get_credentials(old_credentials = Credentials.from_file) ⇒ Object
- #hash_to_params(hash) ⇒ Object
-
#initialize(client_id = , secret = ) ⇒ OAuth
constructor
A new instance of OAuth.
- #token_request_params(old_credentials) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(client_id = , secret = ) ⇒ OAuth
Returns a new instance of OAuth.
19 20 21 22 23 24 25 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 19 def initialize(client_id = ENV["GOOGLE_CLIENT_ID"], secret = ENV["GOOGLE_CLIENT_SECRET"]) if client_id.nil? || secret.nil? raise ArgumentError, "No Google Client ID or Secret was set. These can set in the environment variables \"GOOGLE_CLIENT_ID\" and \"GOOGLE_CLIENT_SECRET\" respectively. Credentials must be created for your project at \"https://console.developers.google.com/apis/credentials\"." end self.client_id = client_id self.secret = secret end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
17 18 19 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 17 def client_id @client_id end |
#secret ⇒ Object
Returns the value of attribute secret.
17 18 19 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 17 def secret @secret end |
Instance Method Details
#get_code ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 27 def get_code puts("Performing OAuth with Google...") if RUBY_PLATFORM =~ /darwin/ system("open \"#{url}\"") else puts("Open this URL in your browser: \"#{url}\"\n\n") end printf "Copy and paste code from web browser here: " _code = STDIN.gets.chomp end |
#get_credentials(old_credentials = Credentials.from_file) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 38 def get_credentials(old_credentials = Credentials.from_file) return old_credentials if old_credentials && !old_credentials.expired? response = Request.post(TOKEN_URI) {|request| request.body = hash_to_params( token_request_params(old_credentials) ) } json = JSON.parse(response.body) credentials = Credentials.new(json) if old_credentials credentials.refresh_token = old_credentials.refresh_token end return credentials end |
#hash_to_params(hash) ⇒ Object
81 82 83 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 81 def hash_to_params(hash) hash.map {|k,v| "#{k}=#{v}"}.join("&") end |
#token_request_params(old_credentials) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 61 def token_request_params(old_credentials) if old_credentials == nil { code: get_code, client_id: client_id, client_secret: secret, redirect_uri: REDIRECT_URI, grant_type: "authorization_code", } else # this is a refresh of the old credentials { refresh_token: old_credentials.refresh_token, client_id: client_id, client_secret: secret, grant_type: "refresh_token", } end end |
#url ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/ephemeral_calc/google_api/oauth.rb', line 51 def url params = hash_to_params( scope: SCOPES.join("%20"), redirect_uri: REDIRECT_URI, response_type: "code", client_id: client_id, ) "https://accounts.google.com/o/oauth2/v2/auth?#{params}" end |