#!/usr/bin/env python3 """ Utility script to check what scopes are available with your current refresh token. This helps diagnose scope-related issues. """ import asyncio import os import sys from datetime import datetime import httpx async def check_token_scopes(client_id: str, client_secret: str, refresh_token: str): """Check what scopes are available with the given refresh token.""" print("๐Ÿ” Checking refresh token scopes...") try: # Refresh the access token async with httpx.AsyncClient() as client: response = await client.post( "https://www.strava.com/oauth/token", json={ "client_id": client_id, "client_secret": client_secret, "refresh_token": refresh_token, "grant_type": "refresh_token", }, ) if response.status_code != 200: print( f"โŒ Failed to refresh token: {response.status_code} - {response.text}" ) return False token_data = response.json() access_token = token_data["access_token"] scope = token_data.get("scope", "unknown") print(f"โœ… Token refresh successful") print(f"๐Ÿ“‹ Available scopes: {scope}") # Check if we have the required scopes required_scopes = ["read", "activity:read"] missing_scopes = [] for req_scope in required_scopes: if req_scope not in scope: missing_scopes.append(req_scope) if missing_scopes: print(f"โŒ Missing required scopes: {', '.join(missing_scopes)}") print("๐Ÿ”ง You need to get a new refresh token with the correct scopes") return False else: print("โœ… All required scopes are present") # Test a simple API call print("\n๐Ÿงช Testing API call to /athlete/activities...") response = await client.get( "https://www.strava.com/api/v3/athlete/activities", headers={"Authorization": f"Bearer {access_token}"}, params={"per_page": 1}, ) if response.status_code == 200: print("โœ… API call successful! Your token works correctly.") activities = response.json() print(f"๐Ÿ“Š Found {len(activities)} activities in test call") return True else: print( f"โŒ API call failed: {response.status_code} - {response.text}" ) return False except Exception as e: print(f"โŒ Error checking token: {e}") return False def main(): """Main function.""" print("๐Ÿ” Strava Refresh Token Scope Checker\n") # Get credentials from environment or command line client_id = os.environ.get("STRAVA_CLIENT_ID") client_secret = os.environ.get("STRAVA_CLIENT_SECRET") refresh_token = os.environ.get("STRAVA_REFRESH_TOKEN") if len(sys.argv) == 4: client_id = sys.argv[1] client_secret = sys.argv[2] refresh_token = sys.argv[3] elif not all([client_id, client_secret, refresh_token]): print("Usage:") print( " python check_token_scopes.py " ) print( " Or set environment variables: STRAVA_CLIENT_ID, STRAVA_CLIENT_SECRET, STRAVA_REFRESH_TOKEN" ) sys.exit(1) # Run the check success = asyncio.run(check_token_scopes(client_id, client_secret, refresh_token)) if not success: print("\n๐Ÿšจ SOLUTION:") print("1. Go to your Hugging Face Space") print("2. Click on the 'OAuth Helper' tab") print( "3. Follow the instructions to get a new refresh token with correct scopes" ) print( "4. Update your STRAVA_REFRESH_TOKEN environment variable or use the Authentication tab" ) sys.exit(0 if success else 1) if __name__ == "__main__": main()