authorization step using Python

643 views
Skip to first unread message

Brett McGregor

unread,
Sep 4, 2018, 7:38:59 PM9/4/18
to Strava API
Hi all,

For all the searching I have done here on this group and across the web, I cannot find a working example of the Strava API authorization process, using Python. I have read the API docs but still do not have the understanding where I can implement what is required. I am very new to this.

If someone could share their code or point me to a resource which will help me get started, then I can begin to understand how the code works. I have a loose understanding of what needs to happen, but like I say, I need a hand getting started.

  1. I have set up my app through my strava account, with the callback domain set as 'localhost'
  2. I have Apache as a local server
here is the starter code I am working with. The two comments in yellow are where I am stuck, I think :)

from stravalib.client import Client
from httplib2 import Http

client = Client()
client_id, client_secret = open('client.secret').read().strip().split(',')
authorize_url = client.authorization_url(client_id=client_id, redirect_uri='http://localhost:8282/authorized') # is the redirect address correct?

# Have the user click the authorization URL, a 'code' param will be added to the redirect_uri
# .....

# Extract the code from your webapp response
code = request.get('code') # what do I do here?!
access_token = client.exchange_code_for_token(client_id=client_id, client_secret=client_secret, code=code)

# Now store that access token somewhere (a database?)
client.access_token = access_token
athlete = client.get_athlete()
print(f"For {athlete.id}, I now have an access token {access_token}")


Brett McGregor

unread,
Sep 5, 2018, 12:17:34 AM9/5/18
to Strava API
OK, so I have managed to get a little further on my own.

I am now receiving the redirect from Strava API to my xampp Apache localhost.

Now I am stuck on how to get the contents of the request back to Python so I can parse out the code!

FYI I have posted a question on Stack Overflow, may be best to answer there if anyone has the answer...

Jonatan Samoocha

unread,
Sep 5, 2018, 2:09:17 AM9/5/18
to Strava API
I'm not sure why you use Python for the token exchange if you already use XAMPP as your web server stack (which includes PHP). If you want to execute your token exchange logic in Python, you should need to run it in a Flask or Django web framework. These are AFAIK not part of XAMPP.

Brett McGregor

unread,
Sep 5, 2018, 4:04:11 PM9/5/18
to Strava API
Hi Jonatan,

Thanks for your message. As you can tell, my understanding of the requirements is not where I would like it to be. However I have seen others carry out the authentication with Strava API purely in Python, but the solution I saw used a Python library to run a local server instead of what I thought may work, i.e. using Apache.

Perhaps I should look into options for running a localhost using Python, then I guess it will be straightforward to capture that response from the API to get the code for exchange. I have done a little bit with Flask...

Atharva Jagtap

unread,
Sep 13, 2018, 3:18:10 PM9/13/18
to Strava API
Hey Brett ,
If you just want to get a access_token to test the API what you can do is compose the url like this 
https://www.strava.com/oauth/authorize?client_id=<clientid>&redirect_uri=https://xxxxxxx.ngrok.io&response_type=code&scope=<scope1>,<scope2>
Then use ngrok and instead of getting localhost just paste the the generated ngrok code in the redirect_uri.
Now navigate to the url you have created using the browser and authorize the app, now you will be redirected to the ngrok and will get a error in browser but don't worry.
After doing the above successfully you can navigate to the localhost:4040(ngrok dashboard) and retrive "code" by looking at the query parameters of the request.
Once you have the code do a post request to the token url using postman and mention the required params.
The response you get in postman now has your access_token.Now you can make request to the strava developer API using the access_token
Of course this approach only works for testing.
Here is the link to the doc that explains what all has to be done http://developers.strava.com/docs/authentication/

Brett McGregor

unread,
Sep 13, 2018, 11:51:14 PM9/13/18
to Strava API
Thank you Atharva, I am playing around with my own Strava data while I fill some gaps in my knowledge of web development frameworks.
I will figure it out.

Regards,
Brett
Reply all
Reply to author
Forward
0 new messages