[Python] cant authenticate with strava api python

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a python script to get data about a specific workout from Strava.

    However my auth token doesn't seem to be working. When I go to the Strava website to get my auth token, and copy the code it gives me when redirected to localhost

    http://localhost/?state=&code=XXXXXXXXXXXXX=read,read_all


    from __future__ import print_function
    import time
    import strava_api_v3
    from strava_api_v3.rest import ApiException
    from pprint import pprint

    # Configure OAuth2 access token for authorization: strava_oauth
    strava_api_v3.configuration.access_token = 'XXXXXXXXXXXXX'
    # create an instance of the API class
    api_instance = strava_api_v3.ActivitiesApi()
    name = 'name_example' # str | The name of the activity.
    type = 'type_example' # str | Type of activity. For example - Run, Ride etc.
    start_date_local = 'start_date_local_example' # str | ISO 8601 formatted date time.
    elapsed_time = 56 # int | In seconds.
    description = 'description_example' # str | Description of the activity. (optional)
    distance = 'distance_example' # str | In meters. (optional)
    private = 56 # int | set to 1 to mark the resulting activity as private, ‘view_private’ permissions will be necessary to view the activity. If not specified, set according to the athlete’s privacy setting (recommended). (optional)
    trainer = 56 # int | Set to 1 to mark as a trainer activity. (optional)
    photo_ids = 'photo_ids_example' # str | List of native photo ids to attach to the activity. (optional)
    commute = 56 # int | Set to 1 to mark as commute. (optional)

    try:
    # Create an Activity
    api_response = api_instance.create_activity(name, type, start_date_local, elapsed_time, description=description, distance=distance, private=private, trainer=trainer, photo_ids=photo_ids, commute=commute)
    pprint(api_response)
    except ApiException as e:
    print("Exception when calling ActivitiesApi->create_activity: %s\n" % e)



    I get this error

    vivaansingh@Vivaans-MacBook-Air-740 strava-api-v3.1-master % /Users/vivaansingh/.pyenv/versions/3.10.13/bin/python /Users/vivaansingh/Downloads/strava-api-v3.1-mas
    ter/main.py
    Exception when calling ActivitiesApi->create_activity: (401)
    Reason: Unauthorized
    HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Date': 'Wed, 18 Jun 2025 21:46:20 GMT', 'x-envoy-upstream-service-time': '7', 'server': 'istio-envoy', 'status': '401 Unauthorized', 'cache-control': 'no-cache', 'vary': 'Accept, Origin', 'referrer-policy': 'strict-origin-when-cross-origin', 'x-permitted-cross-domain-policies': 'none', 'x-xss-protection': '1; mode=block', 'x-request-id': 'e8e75f2a-d7ac-459c-a6a7-55401e69fe35', 'x-download-options': 'noopen', 'x-frame-options': 'DENY', 'x-content-type-options': 'nosniff', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 c513379537c676e2a199d8d4df871de6.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'ORD58-P4', 'X-Amz-Cf-Id': 'dBGhRjtVWllvuMvlZE40dzaO_nf_KYC_TliNGQyUdwc8k8L09wFBGw=='})
    HTTP response body: {"message":"Authorization Error","errors":[{"resource":"Athlete","field":"access_token","code":"invalid"}]}

    Continue reading...

Compartilhe esta Página