-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathstrava-oauth-token
More file actions
executable file
·56 lines (45 loc) · 1.32 KB
/
strava-oauth-token
File metadata and controls
executable file
·56 lines (45 loc) · 1.32 KB
1
2
3
4
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'dotenv/load'
require 'webrick'
require 'strava-ruby-client'
begin
server = WEBrick::HTTPServer.new(Port: 4242)
trap 'INT' do
server.shutdown
end
client_id = ENV.fetch('STRAVA_CLIENT_ID', nil) || raise('Missing STRAVA_CLIENT_ID.')
client_secret = ENV.fetch('STRAVA_CLIENT_SECRET', nil) || raise('Missing STRAVA_CLIENT_SECRET.')
client = Strava::OAuth::Client.new(
client_id: client_id,
client_secret: client_secret
)
server.mount_proc '/' do |req, res|
code = req.query['code']
response = client.oauth_token(code: code)
res.body = %(
<html>
<body>
<ul>
<li>token_type: #{response.token_type}</li>
<li>refresh_token: #{response.refresh_token}</li>
<li>access_token: #{response.access_token}</li>
<li>expires_at: #{response.expires_at}</li>
</ul>
<body>
</html>
)
server.shutdown
end
redirect_url = client.authorize_url(
redirect_uri: 'http://localhost:4242/',
response_type: 'code',
scope: 'read_all,activity:read,activity:read_all,profile:read_all,profile:write,activity:write'
)
server.logger.info "opening browser at #{redirect_url}\n"
system 'open', redirect_url
server.start
rescue Faraday::ClientError => e
puts e.message
puts e.response[:body]
end