5
6
7
8
9
10
11
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
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/box/authorization.rb', line 5
def self.authorize(config = {})
username, password = config[:username], config[:password]
raise "Unable to get auth tokens without username and password" unless username && password
require 'mechanize'
Box.log '... attempting to authorize with username and password'
client_id, client_secret = config[:client_id], config[:client_secret]
agent = Mechanize::new
session = Session.new(config)
authorize_url = session.authorize_url(redirect_uri: 'https://www.chase.com')
login_page = agent.get(authorize_url)
login_form = login_page.form_with(name: 'login_form')
login_form.login = username
login_form.password = password
allow_page = agent.submit(login_form)
consent_form = allow_page.form_with(name: 'consent_form')
accept_button = consent_form.button_with(name: 'consent_accept')
redirpage = agent.submit(consent_form, accept_button)
code_query = CGI::parse(redirpage.uri.query)['code'].to_s
code = code_query[2,code_query.length-4]
session.aquire_access_token(code)
p session.access_token
p session.refresh_token
Box::Session.on_token_refresh.call(session.access_token, session.refresh_token)
return session.oauth2_access_token
end
|