Forum Discussion
Http requests in Fabric Notebook
- Anonymous1 year ago
Hi pbi_taken,
Thank you for reaching out in Microsoft Community Forum.
Please follow below steps to Automating Strava API Data Access in Fabric Notebook
1.The code from the Strava OAuth flow is only needed once to retrieve the initial access_token and refresh_token. After that, you should use the refresh_token to keep getting new access_tokens without requiring the code again.
2.Replace grant_type = 'authorization_code' with grant_type = 'refresh_token' and call the token endpoint:
response = requests.post(
'https://www.strava.com/oauth/token',
data={
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
)3.If you're getting 401 errors, check the access_token permissions. You must have activity:read_all scope granted during the initial authorization. If the token lacks required scopes, reauthorize manually with:
4.Add debugging for response.status_code and print response.json() to catch and understand errors:
if response.status_code != 200:
print("Error during token fetch:", response.status_code, response.json())If the issue still persists,In this scenario we suggest you to raise a support ticket here. so, that they can assit you in addressing the issue you are facing. please follow below link on how to raise a support ticket:
How to create a Fabric and Power BI Support ticket - Power BI | Microsoft Learn
Hi pbi_taken,
Thank you for reaching out in Microsoft Community Forum.
Use Strava’s refresh token flow to automate getting new access tokens — no need to manually get the authorization code every time.
Please follow below steps to resolve the issue;
1.Manually authorize once using the URL → get:
access_token
refresh_token
expires_at
2.In your Fabric notebook, use this refresh_token to automatically get a new access_token:
response = requests.post(
'https://www.strava.com/oauth/token',
data={
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
)
3.Use the new access_token to call Strava APIs.
Please continue using Microsoft community forum.
If you found this post helpful, please consider marking it as "Accept as Solution" and give it a 'Kudos'. if it was helpful. help other members find it more easily.
Regards,
Pavan.
Hi,
thanks! I've tried this now. just changing my code with what you suggested. Doesn't seem to work when converting it to a dataframe..
Any idea why?
My code is below..
response = requests.post(
'https://www.strava.com/oauth/token',
data={
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
)
token_response = response.json()
access_token = token_response['access_token']
headers_act = {
'Authorization': f'Bearer {access_token}'
}
training_response = requests.get('https://www.strava.com/api/v3/athlete/activities', headers=headers_act)
activities = training_response.json()
df_pd = pd.DataFrame(activities)
print(df_pd)
message errors
0 Authorization Error {'resource': 'AccessToken', 'field': 'activity...- Anonymous1 year agoNot applicable
Hi pbi_taken,
Thank you for reaching out in Microsoft Community Forum.
Please follow below troubleshooting steps to resolve the error;
1.Print the response from the token refresh request to verify you're receiving a valid access_token. Use print(response.json()).
2.Make sure the refresh token is valid and hasn’t expired. If it's invalid, you'll need to go through the authorization flow again.
3.Make sure that the access_token has the necessary permissions (scopes) to access the Strava API endpoint, such as activity:read_all.
4.Add error handling to both the token refresh request and activities request to catch and print any errors, and check the status_code for any issues:
if response.status_code != 200:
print(f"Error: {response.status_code}")
print(response.json())Please continue using Microsoft community forum.
If you found this post helpful, please consider marking it as "Accept as Solution" and give it a 'Kudos'. if it was helpful. help other members find it more easily.
Regards,
Pavan.- pbi_taken1 year agoHelper I
this url:
'strava.com/api/v3/athlete/activities'
which I need i requires both access_token and code, which is what I'm getting from the localhost url... This is what I'm getting without inputting code..
{"message":"Authorization Error","errors":[{"resource":"Athlete","field":"access_token","code":"invalid"}]}from what i understand, there is no way of getting around that..
- Anonymous1 year agoNot applicable
Hi pbi_taken,
Thank you for reaching out in Microsoft Community Forum.
You do not need the original code again once you've received the refresh_token from the first manual authorization.
Please use the below steps to automate;
1.Use the refresh_token to get a new access_token:
response = requests.post(
'https://www.strava.com/oauth/token',
data={
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}
)2.Use only the access_token from that response to call Strava’s API:
headers = {'Authorization': f'Bearer {access_token}'}
requests.get('https://www.strava.com/api/v3/athlete/activities', headers=headers)Please continue using Microsoft community forum.
If you found this post helpful, please consider marking it as "Accept as Solution" and give it a 'Kudos'. if it was helpful. help other members find it more easily.
Regards,
Pavan.