Saturday 11 February 2017

Strava Data Analysis - A PC Based Visual C# Solution - V1.1.0.8 - Updates Feb 1017

Updates of Note Dec 2017

Please see the latest version here: http://midlandscyclist.blogspot.co.uk/2017/12/strava-data-analysis-pc-based-visual-c.html


This post has some useful information but refer to the latest version above

Updates of Note Feb 11 2017
  • Detailed instructions on how to read data that was collected in your privacy zone (if you have set one)
  • Now reads power, temperature and smoothed gradient values in the stream data
  • Small other changes
Version 1.1.0.8 can be downloaded here: link

See other posts here: Post 1 and here: Post 2

After several internal versions and having got to the point to being able to extract and export "stream data" which I then viewed in Excel I realised that I was always missing the beginning and end of the data when I was looking at rides when recorded starting from home. It took a a while to realise that this was because I have set a privacy zone and the default Access Token only gives access to my public data. A lot of research and coding has so far failed to address this in any meaningful way. However I now understand the process and and have been able to get a method to give an Access Token for the full stream data. Its quite involved and is laid out as follows.

The first step is to go the the Authorisation page of your application:



In my case I have set the Authorisation Callback Domain as "localhost":


When I select the Authorise button Strava tries to open a  web page on localhost. This however causes an "404 - Not Found" error page to be opened as there is no server/application to respond:


However you do get back the interim token you need. This is shown in the page address bar of your browser:


You need to copy and keep this code - all the text after "&code=".

Next we need to send this code, together with the application ID and the"Client Secret" Token to Strava to get another Access Token which will be the one that we need.

The Strava API help pages describe this method using techniques I am not familiar with. It seem to use "CURL" which appears to be a means of sending web requests without a browser. I failed to achieve this with many iterations and trials.

I then found another method which was to create a small web page (local file) and open this page in your browser.

This is the code for the webpage:

<html>
<body>
<form action="https://www.strava.com/oauth/token" method="POST">
Client Id
<input type="text" name="client_id" value="YOUR_CLIENT_ID">
<br><br>
Client Secret
<input type="text" name="client_secret" value="YOUR_CLIENT_SECRET">
<br><br>
Code
<input type="text" name="code" value="CODE_FROM_PREVIOUS_STEP">
<br><br>
<input type="submit" value="Submit"></form>
</body>
</html>
 
You can dowload it from here: link

You then enter your client ID, Client Secret and the interim token above (I have obscured my private data in the screen shot below. Also as I iterated several times and then created the screen shots for this Blog later, the access token below is not the same as the screen shot above - but normally it would be)
   
 Paste in the access token and press Submit:
  
You'll then get a file back from Strava - save this file and then open it. In this instance it opened in Visual Studio so I assume that the ".json" extension is associated with this application:



The first response is a new access token (obscured in the screen shot above) which is for this user and allows access to their private data - its persistent so there's no need to repeat the process more than once as long as you keep the key (and in fact my application will store this in an .ini file). Copy and paste in to my application next time you run it.

I am looking at how to do this programmatically as this method is pretty user unfriendly.

Once I had the stream data I also found that 3 of the data streams for power, temperature and smoothed  gradient were not being acquired. Further analysis between the Strava documentation and the StravaDotNet packaqe suggested that Strava had altered the descriptive names of these 3 streams, namely from Watts to watts (power), from Temperature to temp and from GradientSmooth to gradient_smooth. I was using StravaDotNet as a "package" so it was a "black box" and I could not edit the code.

Whilst the source for StravaDotNet is supplied I did not know how to rebuild it as a package.

Some considerable iterations later I have worked out how to stop using it as a package and use the source files "as is". Once I had worked this out I could run through the source files and rename "Watts" to "watts" etc. Once I had made these relatively small changes (although a lot of work was required to get to that point) I could then get this previously missing data into the application. 

In this case I had written the code relatively well (I used a few classes) so extending the code to process 3 more streams was actually quite quick. Whether its well written code or could be better is a different matter of course

Further work:
Get the application to authorise directly without needing to do all the above
Filter data by activity type
Error checking
Some graphics
Get segments from an activity and segment data