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 "\nAuthorization was successful. These are your credentials to initialize the library:\n\nJSON format, for credentials.json:\n\n{\n\"client_id\": \"\#{client_id}\",\n\"client_secret\": \"\#{client_secret}\",\n\"access_token\": \"\#{credentials['access_token']}\",\n\"refresh_token\": \"\#{credentials['refresh_token']}\"\n}\n\nYAML format, for imgur.yml:\n\nclient_id: \#{client_id}\nclient_secret: \#{client_secret}\naccess_token: \#{credentials['access_token']}\nrefresh_token: \#{credentials['refresh_token']}\n\n"
end
|