Import and Set Up Authentication ERROR: Failed to fetch profile: (401) Reason: Unauthorized

import upstox_client
from upstox_client import Configuration, UserApi
from upstox_client.rest import ApiException

Initialize with sandbox mode and your valid sandbox token

cfg = Configuration(sandbox=True)
cfg.access_token = “xyz” # generated from dashboard

Create the User API client

user_api = UserApi(upstox_client.ApiClient(cfg))

try:
# Fetch profile (you must pass API version, e.g., “2.0”)
profile_resp = user_api.get_profile(api_version=“2.0”)
name = profile_resp.data.user_name # field containing your name
print(“Login successful as:”, name)
except ApiException as e:
print(“Failed to fetch profile:”, e)