Module: Imgurapi::Rake

Extended by:
Rake
Included in:
Rake
Defined in:
lib/imgurapi/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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imgurapi/tasks/rake.rb', line 12

def authorize(client_id, client_secret)
  puts "\nVisit this URL: #{Imgurapi::Session::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 = Faraday.new(Imgurapi::Session::HOST).post(TOKEN_ENDPOINT, pin: pin, client_id: client_id, client_secret: client_secret, grant_type: 'pin')

    raise "HTTP #{response.status}" if response.status != 200
    credentials = JSON.parse response.body
  rescue => e
    puts "Authorization failed: #{e.message}\nPlease try again."
    exit
  end

  puts <<-MESSAGE

Authorization was successful. These are your credentials to initialize the library:

JSON format, for credentials.json:

{
"client_id": "#{client_id}",
"client_secret": "#{client_secret}",
"access_token": "#{credentials['access_token']}",
"refresh_token": "#{credentials['refresh_token']}"
}

YAML format, for imgur.yml:

client_id: #{client_id}
client_secret: #{client_secret}
access_token: #{credentials['access_token']}
refresh_token: #{credentials['refresh_token']}

MESSAGE
end