Skip to content

Commit 21d692c

Browse files
committed
Added bin/strava-refresh-token.
1 parent 324ff31 commit 21d692c

File tree

5 files changed

+108
-39
lines changed

5 files changed

+108
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 3.0.0 (Next)
22

3+
* [#97](https://github.com/dblock/strava-ruby-client/pull/97): Added `bin/strava-refresh-token` - [@dblock](https://github.com/dblock).
34
* [#96](https://github.com/dblock/strava-ruby-client/pull/96): Update and refactor models to spec - [@dblock](https://github.com/dblock).
45
* [#94](https://github.com/dblock/strava-ruby-client/pull/94): Adds video fields to `Strava::Models::Photo` - [@dblock](https://github.com/dblock).
56
* [#92](https://github.com/dblock/strava-ruby-client/pull/92): Fixes `Hashie::Trash` serialization warning for `object_id` of `Strava::Webhooks::Models::Event` - [@simonneutert](https://github.com/simonneutert).

CONTRIBUTING.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,37 @@ bundle exec rake
2929

3030
### Obtain a Strava Token
3131

32-
The token from the Strava website does not have enough permissions to retrieve your own activities.
33-
Use the [strava-oauth-token tool](#strava-oauth-token) to obtain a short lived with more access scopes.
32+
The token from the Strava website does not have enough permissions to retrieve your own activities, therefore you need to complete the OAuth workflow with additional permissions.
33+
34+
Use the [strava-oauth-token tool](#strava-oauth-token) once.
3435

3536
Obtain `STRAVA_CLIENT_ID` and `STRAVA_CLIENT_SECRET` from [My API Application](https://www.strava.com/settings/api).
3637

3738
```bash
3839
export STRAVA_CLIENT_ID=...
3940
export STRAVA_CLIENT_SECRET=...
41+
4042
bundle exec ruby bin/strava-oauth-token
4143
```
4244

43-
This will open a browser window. Complete the OAuth workflow and note `access_token`.
45+
This will open a browser window. Complete the OAuth workflow and note `refresh_token` and `access_token`. Set `STRAVA_ACCESS_TOKEN` to the value of `access_token`.
4446

4547
```
4648
export STRAVA_ACCESS_TOKEN=...
4749
```
4850

51+
You can repeat the process above when the token expires, or use the `refresh_token`, which is faster.
52+
53+
```bash
54+
export STRAVA_CLIENT_ID=...
55+
export STRAVA_CLIENT_SECRET=...
56+
export STRAVA_API_REFRESH_TOKEN=...
57+
58+
bundle exec ./bin/strava-refresh-token
59+
```
60+
61+
Set `STRAVA_ACCESS_TOKEN` to the value of `access_token`.
62+
4963
### Create a Topic Branch
5064

5165
Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,30 @@ Note that the token from the Strava website does not have enough permissions to
104104
```bash
105105
export STRAVA_CLIENT_ID=...
106106
export STRAVA_CLIENT_SECRET=...
107+
107108
bundle exec ruby bin/strava-oauth-token
108109
```
109110

110-
Note the `access_token` from the browser window.
111+
Set `STRAVA_ACCESS_TOKEN` to the value of `access_token` from the browser window and save `refresh_token` in a secure location.
111112

112113
```bash
113114
export STRAVA_ACCESS_TOKEN=...
115+
114116
bundle exec ruby bin/strava-activities.rb
115117
```
116118

119+
You can repeat the process above when the token expires, or use the `refresh_token`, which is faster.
120+
121+
```bash
122+
export STRAVA_CLIENT_ID=...
123+
export STRAVA_CLIENT_SECRET=...
124+
export STRAVA_API_REFRESH_TOKEN=...
125+
126+
bundle exec ./bin/strava-refresh-token
127+
```
128+
129+
Set `STRAVA_ACCESS_TOKEN` to the value of `access_token`.
130+
117131
### Activities
118132

119133
#### Create an Activity

bin/strava-oauth-token

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,52 @@ require 'dotenv/load'
55
require 'webrick'
66
require 'strava-ruby-client'
77

8-
server = WEBrick::HTTPServer.new(Port: 4242)
8+
begin
9+
server = WEBrick::HTTPServer.new(Port: 4242)
910

10-
trap 'INT' do
11-
server.shutdown
12-
end
11+
trap 'INT' do
12+
server.shutdown
13+
end
1314

14-
client = Strava::OAuth::Client.new(
15-
client_id: ENV.fetch('STRAVA_CLIENT_ID', nil),
16-
client_secret: ENV.fetch('STRAVA_CLIENT_SECRET', nil)
17-
)
18-
19-
server.mount_proc '/' do |req, res|
20-
code = req.query['code']
21-
response = client.oauth_token(code: code)
22-
23-
res.body = %(
24-
<html>
25-
<body>
26-
<ul>
27-
<li>token_type: #{response.token_type}</li>
28-
<li>refresh_token: #{response.refresh_token}</li>
29-
<li>access_token: #{response.access_token}</li>
30-
<li>expires_at: #{response.expires_at}</li>
31-
</ul>
32-
<body>
33-
</html>
34-
)
15+
client_id = ENV.fetch('STRAVA_CLIENT_ID', nil) || raise('Missing STRAVA_CLIENT_ID.')
16+
client_secret = ENV.fetch('STRAVA_CLIENT_SECRET', nil) || raise('Missing STRAVA_CLIENT_SECRET.')
3517

36-
server.shutdown
37-
end
18+
client = Strava::OAuth::Client.new(
19+
client_id: client_id,
20+
client_secret: client_secret
21+
)
3822

39-
redirect_url = client.authorize_url(
40-
redirect_uri: 'http://localhost:4242/',
41-
response_type: 'code',
42-
scope: 'read_all,activity:read,activity:read_all,profile:read_all,profile:write,activity:write'
43-
)
23+
server.mount_proc '/' do |req, res|
24+
code = req.query['code']
25+
response = client.oauth_token(code: code)
26+
27+
res.body = %(
28+
<html>
29+
<body>
30+
<ul>
31+
<li>token_type: #{response.token_type}</li>
32+
<li>refresh_token: #{response.refresh_token}</li>
33+
<li>access_token: #{response.access_token}</li>
34+
<li>expires_at: #{response.expires_at}</li>
35+
</ul>
36+
<body>
37+
</html>
38+
)
39+
40+
server.shutdown
41+
end
42+
43+
redirect_url = client.authorize_url(
44+
redirect_uri: 'http://localhost:4242/',
45+
response_type: 'code',
46+
scope: 'read_all,activity:read,activity:read_all,profile:read_all,profile:write,activity:write'
47+
)
4448

45-
server.logger.info "opening browser at #{redirect_url}\n"
46-
system 'open', redirect_url
49+
server.logger.info "opening browser at #{redirect_url}\n"
50+
system 'open', redirect_url
4751

48-
server.start
52+
server.start
53+
rescue Faraday::ClientError => e
54+
puts e.message
55+
puts e.response[:body]
56+
end

bin/strava-refresh-token

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require 'dotenv/load'
5+
require 'webrick'
6+
require 'strava-ruby-client'
7+
8+
begin
9+
client_id = ENV.fetch('STRAVA_CLIENT_ID', nil) || raise('Missing STRAVA_CLIENT_ID.')
10+
client_secret = ENV.fetch('STRAVA_CLIENT_SECRET', nil) || raise('Missing STRAVA_CLIENT_SECRET.')
11+
refresh_token = ENV.fetch('STRAVA_API_REFRESH_TOKEN', nil) || raise('Missing STRAVA_API_REFRESH_TOKEN.')
12+
13+
client = Strava::OAuth::Client.new(
14+
client_id: client_id,
15+
client_secret: client_secret
16+
)
17+
18+
response = client.oauth_token(
19+
refresh_token: refresh_token,
20+
grant_type: 'refresh_token'
21+
)
22+
23+
puts 'The Strava API refresh token has changed, it will need to be updated.' if refresh_token != response.refresh_token
24+
25+
puts "token_type: #{response.token_type}"
26+
puts "refresh_token: #{response.refresh_token}"
27+
puts "access_token: #{response.access_token}"
28+
puts "expires_at: #{response.expires_at}"
29+
rescue Faraday::ClientError => e
30+
puts e.message
31+
puts e.response[:body]
32+
end

0 commit comments

Comments
 (0)