A information on find out how to authenticate, obtain and add actions between Strava API and a Xamarin.Varieties cell app
I’ve been engaged on a cell app that may be a health exercise tracker and, one of many backlog objects of this mission is to synchronize with Strava API. There are some .NET initiatives on GitHub addressing this challenge, nevertheless, they didn’t slot in my wants. A few of them are outdated, don’t deal with the authentication course of, or lack of documentation. So I made a decision to share what I did and assist anybody going via this.
Authentication is the primary factor you have to do as a way to permit your customers to attach with their Strava account. That is achieved through OAuth 2.0 — a nicely know customary used to grant entry to functions. Let’s assume you have already got a Strava account with an app configured. I received’t cowl this step as a result of there’s numerous materials on the web (test this video for assist). The authorization course of works this manner:
- The app opens a browser and navigates to a web page the place the person is requested to enter his credentials.
- The person enters credentials and is requested to authorize the app to entry his knowledge on Strava.
- On success, the browser is redirected to a web page specified by you (callback area). There can be a URL parameter referred to as code.
- The app detects the redirection, will get the code, and the browser is closed.
- The app calls the API and exchanges the code to entry the token and refreshes the token.
The principle challenge right here is find out how to open the browser and look forward to name again (step 1). Fortunately, the category
Xamarin.Necessities can provoke this stream and hear for a callback (check this link to arrange in initiatives). Test beneath how to do that:
string authURL =
"&scope=exercise:read_all,exercise:write";WebAuthenticatorResult authResult =
One other vital half is the redirect URI. Within the Strava account you’ll set the callback area like myapp.com, so when the authorization happens, the browser will head to one thing like
myapp.com?code=xyz (step 3). Nevertheless, on app, we have to set the redirect URI to
myapp://myapp.com, as a result of when configuring
WebAuthenticator we’ll use the url scheme
myapp (this topic helped me work out this).
Now you may get the code from
authResult.Properties[“code”] and transfer to final step the place you alternate the code to entry token. At this level you ought to be accustomed to Postman (or one other API software) and have the ability to take a look at the next cURL request (check here of find out how to import it on Postman):
curl -X POST https://www.strava.com/api/v3/oauth/token
When efficiently accomplished, the response will include some attributes which I counsel you save in your utility properties or database. They’re
refresh_token. Now you need to use the access_token to request API, however this token will expire quickly (after 6 hours in accordance Strava docs). So if it’s nonetheless legitimate, use it. If not, use the
refresh_token to get a brand new one.
Okay, you bought the code, exchanged it for token and now the token will not be legitimate anymore! Appears sophisticated if it’s your first time doing this, however upon getting a token, it is only a matter of understanding if it’s legitimate or not. And if it’s not, it’s essential to use refresh token asking for a brand new one:
if (tokenExpiresAt > DateTime.Now)
// Ask for a brand new access_token
Each time you have to make a name to Strava API, it is best to test if the token is legitimate, so I like to recommend creating a technique that handles this. When you get a brand new token, it is going to come together with one other refresh token and an expiring date. Then override the outdated ones in your utility storage.
Strava API has an endpoint to list athlete activities that settle for a parameter “after” to filter actions which have taken place after a sure time:
"/athlete/actions?after=" + stravaSyncDate
Having downloaded the actions, simply replace the variable to the precise date/time. Thus, subsequent time you’ll get solely new actions after final sync.
You might also need to obtain different exercise knowledge like coronary heart charge or distance. This sort of knowledge is offered in endpoint Streams. This endpoint accepts a question parameter
keys, by which you inform desired stream varieties. In my case, I wanted coronary heart charge and time, so I made a request with
Every stream incorporates the sequence of values and an attribute
series_type, that’s the base collection used within the case the stream was downsampled (will be distance or time). For coronary heart charge, the collection kind is time, so the sequences match with one another:
"knowledge": [ 74, 75, 76 ],
"knowledge": [ 0, 5, 10 ],
Chances are you’ll assume that to add an exercise, simply make POST calls to the endpoints above. Though the exercise endpoint permits this operation, it’s not doable to add streams. However it’s doable to put up a knowledge file to endpoint
They settle for some codecs and I ended up selecting the GPX which is principally an XML file. I do know there are XML libraries over there, however on this case the construction could be very easy and you’ll simply create one concatenating some string chunks (check my suggestion on StackOverflow).
On this article, I defined find out how to authenticate, obtain and add actions to Strava utilizing
Xamarin.Varieties cell app. I attempted to maintain it succinctly displaying simply the important supply code, however try my GitHub repository for a full model of it. Be happy to achieve me in case of any doubts.