Strava_API sample code for a javascript based web app

597 views
Skip to first unread message

Scott Tomassetti

unread,
Apr 11, 2020, 5:04:59 AM4/11/20
to Strava API
I have been struggling with this for a while now, and really need to see a full example of how to authorize a strava athlete, retrieve their profile data and display it on a page, also the same workflow for getting all activities for an authorized athlete. I always get denied access error codes.
I have tried to follow Fran Polignano youtube video but without success: https://youtu.be/sgscChKfGyg.
Any Help would be appreciated!

Omri l

unread,
Apr 11, 2020, 7:17:10 AM4/11/20
to Strava API
I think I have what you need:
lets take it one step on a time, first you need to build your registration form, that can easily done, and gere a sample:

<form action="https://www.strava.com/oauth/authorize" method="get" id="myform">
<input name="redirect_uri" value="[your final url for the registration]"/>
<input name="client_id" value="[your client Id]"/>
<input name="response_type" value="code"/>
<input name="scope" value="activity:read"/>
<input name="approval_prompt" value="force"/>
<input type="submit" value="Submit from"/>
</form>

code here...

when the form submitted - the user will be redirected to strava auth page, I guess this is no need to explain.
but the second part is:
one the user approve the app for his Strava account he will redirected back to the URL you've entered in the redirect_uri in the form. 
there you'll have to do something like that:

first you need to extract the code out of the response from strava, this will be in the queryString
thank you need to create an http request to strava with that code and your clientId and client_secret, the request will be something like this: (nodejs)
request.post({
url: 'https://www.strava.com/api/v3/oauth/token',
form: {
client_id: '[your client id]',
client_secret: '[your client secret]',
code: data.code,
grant_type: 'authorization_code'
}
}, function (err, httpResponse, body) {
let jsonData = JSON.parse(body);
}
code here...

now you have a json object with all the data you need:
access_token: jsonData.access_token, refresh_token: jsonData.refresh_token, stravaId: jsonData.athlete.id

now you have completed the registration for the user, 
note that the access token is the one you need to use to get the user activities and the refresh token will be use to get new access token once the old one is expired.

Scott Tomassetti

unread,
Apr 11, 2020, 9:33:52 AM4/11/20
to Strava API

Thank you for the great reply and is very helpful, do you have the completed code on GitHub as a repo with html/js.  I need to see a working sample, of course without the OAuth ID's.

Omri l

unread,
Apr 11, 2020, 9:48:15 AM4/11/20
to Strava API
sorry, but my repo have to many other logics, it will not do any good to look into it.
try work with that document and feel free to ping me with questions.

Scott Tomassetti

unread,
Apr 11, 2020, 10:31:51 AM4/11/20
to Strava API
Can you post a sample page using the registration form and js code that works for you without you stravaAPI codes, complete HTML and JS, no need for css or seperate files.  I tried to use your code but received error AGAIN, very frustrating for me!!!!!

Scott Tomassetti

unread,
Apr 11, 2020, 10:33:17 AM4/11/20
to Strava API
At this point I would be happy to pay for a working sample, SOMEBODY!  

Omri l

unread,
Apr 11, 2020, 10:37:39 AM4/11/20
to Strava API
can you tell me what error did you get?
did you set the Authorization Callback Domain at https://www.strava.com/settings/api to be the same as your domain, where the HTML form is?
Reply all
Reply to author
Forward
0 new messages