Sunday 7 April 2019

Automatic Programmatic Authentication to Strava in C#

Note: this page was started 7 April 2019 and has been updated 22 April.

This latest update is to confirm that this code gets the correct access and refresh tokens which, when used to connect to Strava, will get data according to the Scope selected. 

I was getting a "401" error when trying to get Activity data. However this was because I was using
Scope: read_all which reads "private routes, private segments, and private events for the user".
I.e. it does not read Activities for which you need:
Scope: activity:read_all "which is the same access as activity:read (i.e. read the user's activity data for activities that are visible to Everyone and Followers, excluding privacy zone data), plus privacy zone data and access to read the user's activities with visibility set to Only You"

This means that this code is a functional (if inelegant, badly written, poorly documented etc) route to get the access tokens needed from October 2019

The Windows PC programme I have written in C# to get my data from Strava and display and analyse in a variety of ways (latest Blog post here: link) has a relatively major failing - that is that it is necessary to manually go through a number of rather non-intuitive steps to get a Strava Access Token. Although its not elegant it was a one-off task until this year. 

In October 2019 the Strava Authentication method is changing from a non-expiring token (as per my application when I created it) to a time limited token process which then involves re-authenticating every 6 hours. This is described here: link

Whilst my application will continue to work (for me) until Oct 2019, using my original non-expiring token, at this point I will need to move to the new process of this time limited token exchange.

Anyone wanting to use my application now and follow the process I have documented in the blog will find that they they probably can manually get a token using a similar process it will be a pain and then that token will expire 6 hours later.

Therefore to address this I have started to develop a proper authentication solution as I have no choice. I approached this by writing a separate application to test the process and methodology, and then once I had a working system integrate into my original application. I also created a separate Strava account to test this with rather than risk corrupting my current account access and losing my data (more on this later).

As before I created (in my new account) an application and registered it. This is documented here: link and here: link

I then spent some time reading the documentation to see how it worked. I also manually typed in the html code/links directly into a browser to look at the response so I understood what you needed to send and what answer you got back


This a simplified flow diagram of how I believe it works:




There are some prerequisites -like needing a local web server running on your PC to receive the initial Authorisation code

I found a usable sample here: Simple C# Web Server. I don't understand how it works - all I know is that it does. Start the Web server before opening the web page, close it once you have a response. Note that this will open a web page and show the response rather than silently interpreting the reply.

This "waiting for a response" behaviour here also caused issues. We need to await the user authorising the Strava Request - at this point we are no longer in a pure Synchronous programming mode.

To address this I have user a timer to await the web server responding - this loops until either a timeout is reached or a successful authentication is achieved. I tried to use async and await but could not get this to work - I need to do more research and some other programme examples. Once we have the response from the Web server we can post to the Strava oauth link and get the short term access codes.

I believe that I have created a robust piece of code that does all of that and I wrote this in a small test application:




This application robustly returns codes and an expiry time. Initial testing in the main Strava application however got a "404 not authorised" response - this is where more testing is required, but as explained above has now been resolved by selecting the appropriate scope

Don't bother using these tokens as they are already expired!

The source code is here: Source Code. In theory, unzip the file, copy the contents into a folder, and using Visual Studio Open the solution "Strava_Autenticate_V3" (I could not spell "Authenticate" correctly!). Compile/build and it should run

Any feedback I would welcome, especially if there is an error.

Please note I am not a software developer - I am an engineer who can write software - I know there is a big difference. Again - for my poor design and coding - I am happy to receive constructive feedback







No comments:

Post a Comment