Getting Started Using The Strava API

avatar

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.

Introduction


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.

Getting Started On Strava


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.

Create the URL for users to authenticate with Strava and your App


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

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.

Receive the call back URL, including the one time Strava token


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

Use the one time Strava token to authenticate with Strava


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,
…
}

Use the access code to generate a list of activities


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,
...

Use the refresh token to update the access code when it expires


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



0
0
0.000
9 comments
avatar

Ooh it makes sense
This is a perfect explanation
I'd be there!

0
0
0.000
avatar

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

0
0
0.000
avatar

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.

0
0
0.000
avatar

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,

0
0
0.000
avatar

Congratulations @strava2hive! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You received more than 4250 upvotes.
Your next target is to reach 4500 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Women's World Cup Contest - Round of 16 - Recap of Day 4
Women's World Cup Contest - Round of 16 - Recap of Day 3
Women's World Cup Contest - Round of 16 - Recap of Day 2
0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord 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. 
 

0
0
0.000
avatar

Sorry, but I do not really understand why it is good. Will the workout analysis be seen on hive, as well?

0
0
0.000
avatar

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.

0
0
0.000
avatar

Okay, I understood so far. Thank you for your work on this coolen stuff, to take Strava to hive .

0
0
0.000