cancel
Showing results for 
Search instead for 
Did you mean: 

Invalid Authorization Code After 1 Use

Fizzlepie
Mt. Kenya

I was trying to develop my code (in Python) to gather my club's data for a data science course project. The issue I have is that I'm learning that once I run the request using the authorization code one time, the second time won't work. Even if I get my access and refresh token, running the code in the main will end up running the function to use the authorization code so it'll become invalid and the code won't run. How do I solve this?

Here is a picture to show what I mean:

Fizzlepie_0-1700195576803.png

 

8 REPLIES 8

Fizzlepie
Mt. Kenya

Hey livb, thanks for your response! I was able to figure out how to go about calling for the refresh code which you can then use to get a new access code whenever you want to get the data again. 

This line of code will use the authorization code to give you the refresh and access token:

payload = {"client_id" : client_id,
"client_secret" : client_secret,
"code" : code_val,
"grant_type" : "authorization_code",
"f" : "json"
}

res = requests.post(auth_url, data = payload, verify = False).json()

res["refresh_token"] will let you store the refresh token

 

To get a new access code, you'll want to do a similar thing:

payload = {"client_id" : client_id,
"client_secret" : client_secret,
"refresh_token" : refresh_token
"grant_type" : "refresh_token",
"f" : "json"
}
# GET ACCESS TOKEN USING REFRESH TOKEN
res = requests.post(auth_url, data = payload, verify = False).json()
access_token = res["access_token"]


# RETRIEVING ACTIVITY DATA FROM STRAVA
header = {'Authorization': 'Bearer ' + access_token}

param = {'per_page': 1, 'page': 1}
data = requests.get(activities_url, headers=header, params=param).json()

 

I hope this helps! The part I'm stuck on is how I would be able to add the refresh token to my dataframe and then not have to run the code to GET that refresh token using the authorization code when I want to test a new set of code 😕 Sorry if this is poorly worded.

oops meant to send as a reply to livb

unfortunately this results in an error for me and I don't know why

{'message': 'Bad Request', 'errors': [{'resource': 'AuthorizationCode', 'field': 'code', 'code': 'invalid'}]}

If you had already called the authorization code before, I believe you are running into the same issue as me... Once you use it, or it expires, you can't call the authorization code again

thanks no this is when I try to use the refresh token in the same way as you:
payload = {"client_id" : client_id,
"client_secret" : client_secret,
"refresh_token" : refresh_token
"grant_type" : "refresh_token",
"f" : "json"
}
# GET ACCESS TOKEN USING REFRESH TOKEN
res = requests.post(auth_url, data = payload, verify = False).json()
access_token = res["access_token"]

Can I check the refresh_token that you use here...is that the same one given when you were given your last functioning access token?

payload = {
    'client_id': strava_client_id,
    'client_secret': strava_client_secret,
    'code': strava_refresh_token,
    'grant_type ': "authorization_code",
    'f': 'json'
}
try:
    print("Requesting Token...\n")
    res = requests.post(auth_url, data=payload, verify=False)
    activity_access_token = res.json()['access_token']
except:
    auth_url = "https://www.strava.com/api/v3/oauth/token"
    payload = {
            'client_id': strava_client_id,
            'client_secret': strava_client_secret,
            'refresh_token': activity_refresh_token,
            'grant_type ': "refresh_token",
            "f": "json"
            }
    activity_access_token = requests.post(auth_url, data=payload, verify = False).json()
    activity_access_token = activity_access_token['access_token']

livb
Shkhara

you would need to use the refresh code (that you are given with the token) to generate a new token after the initial authorisation. I am however struggling to get that to work... I am hoping someone can help us both!

Hey livb, thanks for your response! I was able to figure out how to go about calling for the refresh code which you can then use to get a new access code whenever you want to get the data again. 

This line of code will use the authorization code to give you the refresh and access token:

payload = {"client_id" : client_id,
"client_secret" : client_secret,
"code" : code_val,
"grant_type" : "authorization_code",
"f" : "json"
}

res = requests.post(auth_url, data = payload, verify = False).json()

res["refresh_token"] will let you store the refresh token

 

To get a new access code, you'll want to do a similar thing:

payload = {"client_id" : client_id,
"client_secret" : client_secret,
"refresh_token" : refresh_token
"grant_type" : "refresh_token",
"f" : "json"
}
# GET ACCESS TOKEN USING REFRESH TOKEN
res = requests.post(auth_url, data = payload, verify = False).json()
access_token = res["access_token"]


# RETRIEVING ACTIVITY DATA FROM STRAVA
header = {'Authorization': 'Bearer ' + access_token}

param = {'per_page': 1, 'page': 1}
data = requests.get(activities_url, headers=header, params=param).json()

 

I hope this helps! The part I'm stuck on is how I would be able to add the refresh token to my dataframe and then not have to run the code to GET that refresh token using the authorization code when I want to test a new set of code 😕 Sorry if that was poorly worded.