Module: SendGmail::Auth

Included in:
Client
Defined in:
lib/send_gmail/auth.rb

Instance Method Summary collapse

Instance Method Details

#authorize(credentials_path = '/tmp/client_secrets.json', client_id = ENV['GOOGLE_CLIENT_ID'], client_secret = ENV['GOOGLE_CLIENT_SECRET'], scope = 'https://www.googleapis.com/auth/gmail.readonly') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/send_gmail/auth.rb', line 9

def authorize(
  credentials_path = '/tmp/client_secrets.json',
  client_id = ENV['GOOGLE_CLIENT_ID'],
  client_secret = ENV['GOOGLE_CLIENT_SECRET'],
  scope = 'https://www.googleapis.com/auth/gmail.readonly'
  )
  
  FileUtils.mkdir_p(File.dirname(credentials_path))

  file_store = Google::APIClient::FileStore.new(credentials_path)
  storage = Google::APIClient::Storage.new(file_store)
  auth = storage.authorize

  if auth.nil? || (auth.expired? && auth.refresh_token.nil?)
    flow = Google::APIClient::InstalledAppFlow.new(
      client_id: client_id,
      client_secret: client_secret,
      scope: scope
    )
    auth = flow.authorize(storage)
  end
  @client.authorization = auth
end