Class: LeanMicrosoftGraph::Resources::TokenResource

Inherits:
LeanMicrosoftGraph::Resource show all
Defined in:
lib/lean_microsoft_graph/resources/token_resource.rb,
lib/lean_microsoft_graph/resources/token_resource/token.rb

Defined Under Namespace

Classes: Token

Instance Method Summary collapse

Methods inherited from LeanMicrosoftGraph::Resource

#get_request, #post_request

Constructor Details

#initialize(credentials, adapter = Faraday.default_adapter, stubs = nil) ⇒ TokenResource

Returns a new instance of TokenResource.



6
7
8
9
# File 'lib/lean_microsoft_graph/resources/token_resource.rb', line 6

def initialize(credentials, adapter = Faraday.default_adapter, stubs = nil)
  super(Faraday.new { |faraday| faraday.adapter(adapter, stubs)})
  @credentials = credentials
end

Instance Method Details

#renewObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lean_microsoft_graph/resources/token_resource.rb', line 11

def renew
  url = "https://login.microsoftonline.com/#{@credentials.tenant_id}/oauth2/v2.0/token"
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
  body = {
    'client_id' => @credentials.client_id,
    'scope' => 'https://graph.microsoft.com/.default',
    'client_secret' => @credentials.client_secret,
    'grant_type' => 'client_credentials'
  }
  response = post_request(url, URI.encode_www_form(body), headers)

  Token.new(response[:access_token], response[:expires_in].to_i)
end