create account

Getting Started Using The Strava API by strava2hive

View this thread on: hive.blogpeakd.comecency.com
· @strava2hive ·
$8.05
Getting Started Using The Strava API
**This article gets you started working with the Strava API. We discuss signing up with Strava to gain access to the API, authenticating with the Strava API and gaining a some of the activities the user has uploaded onto Strava.**

<h2>Introduction</h2>
Just over a year ago, I set up a service within Hive to allow Strava users the ability to get their activities posted to Hive. It's basically called the Strava2Hive service, and if you want to know a little bit more about the service, you can find out more here:
https://hive.blog/hive-176853/@strava2hive/strava2hive-frequently-asked-questions

I have been a little slow in getting work done with the service. The service is stable and I don't really want to do anything to mess up things when they are going well. I thought to try and get myself to code and research new features and upgrades, it would be good to document the process so far, to both help me, or help anyone else wanting to get started doing something similar.

<h2>Getting Started On Strava</h2>
To get started you will need to get access to the Strava application, and from here you will able to also sign up to the Strava API. Please note that Strava have been making some changes to their policies and access, including the number of times you can access the API in an hour, but hopefully it will not lock you out any time soon.

If you don't have an account with Strava, go to the following URL to sign up for an account: https://www.strava.com/register

Once you have registered and you are logged in, go to the settings page, which will let you create an app with your account. This app will then allow you to access and communicate with the Strava API. Click on the following link to start setting it up: https://www.strava.com/settings/api

**When you click on the link, you will be presented with a form called "My API Application" and it will ask for the following details to set up your access, including Application Name, Category, Club, Website, Application Description, Authorisation Callback Domain.**

Everything is pretty straight forward and if you do not have an Authorisation Callback Domain yet while you are building your app, simply set it as "localhost". Make sure you agree with the terms and conditions and then click on the Create button.

The final step is to add an image for your app. You should then have you API page set up.

Strava is getting better at providing documentation and their reference guide for developers is located at the following page: https://developers.strava.com/docs/reference/

You can now return to your API Application page, using the the URL: https://www.strava.com/settings/api

Once you have your API Application set up, you will have all your details at this page, including the following items you will be using to communicate with the Strava API ClientID, Client Secret, Access Token and Refresh Token.

As you may have already noticed, Strava requires authentication via OAuth 2.0 in order to request data about any athlete. OAuth 2.0 allows developers to interact with Strava athletes without having to store sensitive information. When OAuth is initiated, the athlete, including the owner of the API Application, is prompted by the application to log in to the Strava website and give consent to the requesting application. The application must complete the authentication process by exchanging the authorization code for a refresh token and short-lived access token.

The basic steps to then be able to get an athletes Strava activities include:
1. Create the URL for users to authenticate with Strava and your App
2. Receive the call back URL, including the one time Strava token
3. Use the one time Strava token to authenticate with Strava, where the Strava api will assign you an access code, for the specific athlete
4. Use the access code to generate a list of activities
5. Use the refresh token to update the access code when it expires.

<h3>Create the URL for users to authenticate with Strava and your App</h3>
With your Client ID that you now have in your API Application, you can create the following URL that can be used in a web browser to test authenticating with your new API Application:

```
http://www.strava.com/oauth/authorize?client_id=[REPLACE WITH YOUR CLIENT ID]&response_type=code&redirect_uri=http://localhost/exchange_token&approval_prompt=force&scope=activity:read_all 
```

When you open this URL in a web browser you will see the following web page, which allows your application users to authenticate with Strava:

![image (5).png](https://images.hive.blog/DQmQkrKRUVN1rkBqBNb17FTp1efJ4uN4W1n1LuKazWbLwBX/image%20(5).png)

The user needs to click on the Authorize button and if they uncheck any of the check boxes, this could cause issues with any further requests you make to the API. Once the user has Authorized, the user will then be redirected to the callback domain you provided when you created your API Application.

<h3>Receive the call back URL, including the one time Strava token</h3>
The URL you get provided back will include the code you can now use to get an access token for that user, to obtain further information from strava. If will look something like the one below;

```
http://localhost/exchange_token?state=&code=fe0667a43a5810d0c7e6XXXXX&scope=read
```

<h3>Use the one time Strava token to authenticate with Strava</h3>
Using the code returned for this user, you can now provide this to the Strava api and you will be able to receive details on the athlete, as well as an access token to view the athletes activities, an expiry date for the access token, and a refresh token to generate a new access token once it has expired.

We can create the following curl command using, once again your API client ID and client secret, and now the code provided in the previous step. The curl command will look like the one below: 

```
curl -X POST https://www.strava.com/oauth/token \
	-F client_id=<ADD YOUR CLIENT ID> \
	-F client_secret=<ADD YOUR CLIENT SECRET> \
	-F code=<ADD THE CODE PROVIDED FROM THE PREVIOUS STEP> \
	-F grant_type=authorization_code
```

If you run this and are successful, you will get an output similar to the one below:

```
{
  "token_type": "Bearer",
  "expires_at": 1691747933,
  "expires_in": 21450,
  "refresh_token": "9afc48515bXXX”,
  "access_token": "b50186128XXXX”,
  "athlete": {
    "id": 101635754,
    "username": null,
    "resource_state": 2,
…
}
```

<h3>Use the access code to generate a list of activities</h3>
We can now use the access token(You will add in the one you got from your previous query) to then start to look through the athletes activity. The curl command below, needs to use the athlete ID and access token you have just received.

```
curl -G "https://www.strava.com/api/v3/athletes/<ATHLETE ID>/activities?per_page=3" -H "Authorization: Bearer <ACCESS TOKEN>” | jq
```

Once again, if successful, you will see a list of activities from that user. In the truncated example below, we can see the "Morning Walk Home" performed by the athlete:
```
[
  {
    "resource_state": 2,
    "athlete": {
      "id": 101635754,
      "resource_state": 1
    },
    "name": "Morning Walk Home ",
    "distance": 3993.6,
    "moving_time": 2139,
    "elapsed_time": 2139,
    "total_elevation_gain": 7,
    "type": "Walk",
    "sport_type": "Walk",
    "id": 8191775216,
    "start_date": "2022-11-30T21:14:02Z",
    "start_date_local": "2022-12-01T10:14:02Z",
    "timezone": "(GMT+12:00) Pacific/Auckland",
    "utc_offset": 46800,
...
```

<h3>Use the refresh token to update the access code when it expires</h3>
The access tokens are short lived, and from the list time I checked, it was six hours before needing to update your token. You use a similar command to the one you used to get your original access token, but instead, you use the refresh token you were provided originally:

```
curl -X POST https://www.strava.com/api/v3/oauth/token \
  -d client_id=<ADD YOUR CLIENT ID> \
  -d client_secret=<ADD YOUR CLIENT SECRET> \
  -d grant_type=refresh_token \
  -d refresh_token=<ADD THE CODE PROVIDED FROM THE PREVIOUS STEP>
```

***This hasn't been very glamorous, but hopefully it has given you an idea on how to get started working with the Strava API. I am going to try and post to StemGeeks regularly so I can elaborate and provide some more useful examples of how to work with the Strava activity data and hopefully this will then follow through to the @Strava2Hive App.***

If you have any questions or comments, please let me know.

Posted with [STEMGeeks](https://stemgeeks.net)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 571 others
properties (23)
authorstrava2hive
permlinkgetting-started-using-the-strava-api
categoryhive-163521
json_metadata{"tags":["strava2hive","coding","runningproject","alive","aliveandthriving","sportstalk","healthandfitness","exhaust","stem"],"users":["strava2hive"],"image":["https://images.hive.blog/DQmQkrKRUVN1rkBqBNb17FTp1efJ4uN4W1n1LuKazWbLwBX/image%20(5).png"],"links":["https://hive.blog/hive-176853/@strava2hive/strava2hive-frequently-asked-questions"],"app":"stemgeeks/0.1","format":"markdown","canonical_url":"https://stemgeeks.net/@strava2hive/getting-started-using-the-strava-api"}
created2023-08-11 04:58:06
last_update2023-08-11 04:58:06
depth0
children9
last_payout2023-08-18 04:58:06
cashout_time1969-12-31 23:59:59
total_payout_value4.030 HBD
curator_payout_value4.016 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,585
author_reputation30,317,741,951,216
root_title"Getting Started Using The Strava API"
beneficiaries
0.
accounthiveonboard
weight100
1.
accountocdb
weight100
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,143,689
net_rshares19,873,173,465,451
author_curate_reward""
vote details (635)
@edina76 ·
$0.02
Sorry, but I do not really understand why it is good. Will the workout analysis be seen on hive, as well?
👍  , ,
properties (23)
authoredina76
permlinkre-strava2hive-rzbk1p
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.7.1"}
created2023-08-13 07:41:48
last_update2023-08-13 07:41:48
depth1
children2
last_payout2023-08-20 07:41:48
cashout_time1969-12-31 23:59:59
total_payout_value0.010 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length105
author_reputation44,601,311,726,554
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,211,106
net_rshares58,331,588,686
author_curate_reward""
vote details (3)
@run.vince.run ·
Hi @edina76 I am just doing an bit of work on how the @strava2hive application currently works, and my hope is that, this will allow me to find new features to add for users. Hopefully in a few weeks this should make sence, but let me know if you have any more questions.
👍  ,
properties (23)
authorrun.vince.run
permlinkrzblup
categoryhive-163521
json_metadata{"users":["edina76","strava2hive"],"app":"hiveblog/0.1"}
created2023-08-13 08:20:51
last_update2023-08-13 08:20:51
depth2
children1
last_payout2023-08-20 08:20:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length271
author_reputation199,347,435,597,400
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,211,864
net_rshares128,562,323
author_curate_reward""
vote details (2)
@edina76 ·
$0.02
Okay, I understood so far. Thank you for your work on this coolen stuff, to take Strava to hive .
👍  , ,
properties (23)
authoredina76
permlinkre-runvincerun-rzbm3n
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.7.1"}
created2023-08-13 08:26:09
last_update2023-08-13 08:26:09
depth3
children0
last_payout2023-08-20 08:26:09
cashout_time1969-12-31 23:59:59
total_payout_value0.010 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length97
author_reputation44,601,311,726,554
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,211,955
net_rshares54,641,592,889
author_curate_reward""
vote details (3)
@hivebuzz ·
Congratulations @strava2hive! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@strava2hive/upvoted.png?202308112035"></td><td>You received more than 4250 upvotes.<br>Your next target is to reach 4500 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@strava2hive) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out our last posts:**
<table><tr><td><a href="/hive-102201/@hivebuzz/wc2023-recap-day19"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/48kBuTQSRzzteFSojBm1d421vrXMUPHxq7Z9WdpwpNn3E59JAC8rn6ViEafBFPvgkJ.png"></a></td><td><a href="/hive-102201/@hivebuzz/wc2023-recap-day19">Women's World Cup Contest - Round of 16 - Recap of Day 4</a></td></tr><tr><td><a href="/hive-102201/@hivebuzz/wc2023-recap-day18"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/48Un9NAH7ZfZnA9knFJEpZP535q8FcWMEcDkbL9RGeKkRETzuZYYduq2hqyVUwGWfT.png"></a></td><td><a href="/hive-102201/@hivebuzz/wc2023-recap-day18">Women's World Cup Contest - Round of 16 - Recap of Day 3</a></td></tr><tr><td><a href="/hive-102201/@hivebuzz/wc2023-recap-day17"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/48TozstqJfBJAmxL9tcSzUVfRpM7MJ9zhx4efy5S343UMDsA7Sc2oJopDLZ1f7X1LR.png"></a></td><td><a href="/hive-102201/@hivebuzz/wc2023-recap-day17">Women's World Cup Contest - Round of 16 - Recap of Day 2</a></td></tr></table>
properties (22)
authorhivebuzz
permlinknotify-strava2hive-20230811t210757
categoryhive-163521
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2023-08-11 21:07:57
last_update2023-08-11 21:07:57
depth1
children0
last_payout2023-08-18 21:07:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,757
author_reputation369,398,364,177,415
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,164,579
net_rshares0
@kwarkyria ·
$0.02
Thanks for info. I already have my Strava Acc, but not sure the info to put into the *My API App* form. 

>Application Name, Category, Club, Website, Application Description, Authorisation Callback Domain

Any idea?

Thanks in advance 
👍  , ,
properties (23)
authorkwarkyria
permlinkre-strava2hive-rz8no9
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.7.1"}
created2023-08-11 18:07:21
last_update2023-08-11 18:07:21
depth1
children2
last_payout2023-08-18 18:07:21
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length235
author_reputation3,206,000,122,497
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,159,189
net_rshares64,135,357,530
author_curate_reward""
vote details (3)
@kwarkyria ·
$0.02
Well noted! Thanks for your prompt response.

I will try it again and I hope this time the registration is successful.😉

Thank for your time @run.vince.run 

Warm Rgds, 
👍  , ,
properties (23)
authorkwarkyria
permlinkre-kwarkyria-rz8x34
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.7.1"}
created2023-08-11 21:30:42
last_update2023-08-11 21:30:42
depth2
children0
last_payout2023-08-18 21:30:42
cashout_time1969-12-31 23:59:59
total_payout_value0.011 HBD
curator_payout_value0.011 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length169
author_reputation3,206,000,122,497
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,165,258
net_rshares58,775,348,359
author_curate_reward""
vote details (3)
@run.vince.run ·
Hey @kwarkyria sorry I did not explain further. Application name is simply the name of your app. The category is chosen from a drop down list, the club can be left blank, the description is just a basic description of what your app does, and the call back domain is explained above...when you are just doing development you can simply put "localhost"...hope that helps.
👍  ,
properties (23)
authorrun.vince.run
permlinkrz8qy0
categoryhive-163521
json_metadata{"users":["kwarkyria"],"app":"hiveblog/0.1"}
created2023-08-11 19:18:00
last_update2023-08-11 19:18:00
depth2
children0
last_payout2023-08-18 19:18:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length369
author_reputation199,347,435,597,400
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,160,674
net_rshares119,746,247
author_curate_reward""
vote details (2)
@rafzat ·
Ooh it makes sense
This is a perfect explanation 
I'd be there!
👍  ,
properties (23)
authorrafzat
permlinkre-strava2hive-2023811t111959378z
categoryhive-163521
json_metadata{"tags":["hive-163521","strava2hive","coding","runningproject","alive","aliveandthriving","sportstalk","healthandfitness","exhaust","stem"],"app":"ecency/3.0.42-mobile","format":"markdown+html"}
created2023-08-11 10:20:00
last_update2023-08-11 10:20:00
depth1
children0
last_payout2023-08-18 10:20:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length63
author_reputation183,560,271,702,716
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,149,341
net_rshares119,343,242
author_curate_reward""
vote details (2)
@stemsocial ·
re-strava2hive-getting-started-using-the-strava-api-20230812t042942191z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-strava2hive-getting-started-using-the-strava-api-20230812t042942191z
categoryhive-163521
json_metadata{"app":"STEMsocial"}
created2023-08-12 04:29:42
last_update2023-08-12 04:29:42
depth1
children0
last_payout2023-08-19 04:29:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,918,491,691,707
root_title"Getting Started Using The Strava API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id126,175,994
net_rshares0