Class: Mirror::Api::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/mirror-api/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, refresh_token) ⇒ OAuth

Returns a new instance of OAuth.



5
6
7
8
9
# File 'lib/mirror-api/oauth.rb', line 5

def initialize(client_id, client_secret, refresh_token)
  @client_id = client_id
  @client_secret = client_secret
  @refresh_token = refresh_token
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



4
5
6
# File 'lib/mirror-api/oauth.rb', line 4

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



4
5
6
# File 'lib/mirror-api/oauth.rb', line 4

def client_secret
  @client_secret
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



4
5
6
# File 'lib/mirror-api/oauth.rb', line 4

def refresh_token
  @refresh_token
end

Instance Method Details

#get_access_tokenObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mirror-api/oauth.rb', line 11

def get_access_token
  req = Net::HTTP::Post.new("/o/oauth2/token")
  req.set_form_data(client_id: self.client_id, client_secret: self.client_secret, refresh_token: self.refresh_token, grant_type: "refresh_token")
  res = Net::HTTP.start("accounts.google.com", use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |http| http.request(req) }
  result = JSON.parse(res.body)

  if result
    if result["access_token"]
      result["access_token"]
    elsif result["error"]
      raise "Error in get_access_token #{result["error"]}"
    end
  end
end