Module: Imgur::Rake

Extended by:
Rake
Included in:
Rake
Defined in:
lib/imgur/tasks/rake.rb

Constant Summary collapse

AUTHORIZE_ENDPOINT =
'/oauth2/authorize'
TOKEN_ENDPOINT =
'/oauth2/token'

Instance Method Summary collapse

Instance Method Details

#authorize(client_id, client_secret) ⇒ Object



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

def authorize(client_id, client_secret)
  connection = Faraday.new(HOST)

  puts "\nVisit this URL: #{HOST}#{AUTHORIZE_ENDPOINT}?client_id=#{client_id}&response_type=pin"
  print 'And after you approved the authorization please enter your verification code: '

  pin = STDIN.gets.strip

  begin
    response = JSON.parse connection.post(TOKEN_ENDPOINT, pin: pin, client_id: client_id, client_secret: client_secret, grant_type: 'pin').body
  rescue
    puts "Authorization failed.\nPlease try again."
    exit
  end

  puts <<-MESSAGE

Authorization was successful. Use these credentials to initialize the library:

access_token: #{response['access_token']}
refresh_secret: #{response['refresh_token']}

    MESSAGE
end