Cannot do Update Activity in python

179 views
Skip to first unread message

Nick Sciarrilli

unread,
Mar 1, 2021, 12:36:29 PM3/1/21
to Strava API
Hello,

I'm having some issues. I am able to do an Update Activity by Id in postman but am unable to do the same in python. I've tried with the swagger client, urllib3 and requests library. What happens is I get a 200 response and it shows me the activity but it does not update the name. I've tried different scopes and still not luck. I'm all out of ideas here.

Regards,
Nick

Bryant Likes

unread,
Mar 1, 2021, 12:39:33 PM3/1/21
to Nick Sciarrilli, Strava API
Not real familiar with python, but are you passing the activity id as a string? I know that javascript requires a string since the id is too big for a javascript int. 


--
You received this message because you are subscribed to the Google Groups "Strava API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strava-api+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/strava-api/e47eeea0-af50-44e5-b925-11b7aecf47f7n%40googlegroups.com.

Nick Sciarrilli

unread,
Mar 1, 2021, 1:10:53 PM3/1/21
to Bryant Likes, Strava API
So if I'm using swagger client it's asking for a 'long' type. I tried with string and it didn't work. If I'm rolling my own in python the activity id is part of the url which is a string.

Bryant Likes

unread,
Mar 1, 2021, 1:21:34 PM3/1/21
to Nick Sciarrilli, Strava API
Ok, but how are you constructing that url string? You might want to output the url to make sure you're passing the id correctly. 

Nick Sciarrilli

unread,
Mar 1, 2021, 1:23:10 PM3/1/21
to Bryant Likes, Strava API
Check it:

#!/usr/bin/env python3
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

strava_access_token = '###'
api_instance = swagger_client.ActivitiesApi()
api_instance.api_client.configuration.access_token = strava_access_token


# api_instance = swagger_client.ActivitiesApi(swagger_client.ApiClient(configuration))
id = 4850353749 # Long | The identifier of the activity.
body = swagger_client.UpdatableActivity({"name": "what the fuck"})

try:
# Update Activity
api_response = api_instance.update_activity_by_id(id, body=body)
pprint(api_response)
except ApiException as e:
print("Exception when calling ActivitiesApi->updateActivityById: %s\n" % e)

Bryant Likes

unread,
Mar 1, 2021, 1:33:42 PM3/1/21
to Nick Sciarrilli, Strava API
I can't see what the URL is from that code. I would try debugging it and compare the url + body in python to what works in postman. There has to be some difference since one works and the other one doesn't. 

Nick Sciarrilli

unread,
Mar 1, 2021, 1:35:21 PM3/1/21
to Bryant Likes, Strava API
The url on that is embedded probably in the swagger library. Here is the roll my own version with urllib3

#!/usr/bin/env python3
import json
import urllib3

http = urllib3.PoolManager()

activity_id = '4869932316'
access_token = '####'


body = {'name': 'fishy fishy', 'description': "nohello"}
encoded_data = json.dumps(body)


r = http.urlopen('PUT', url, body=encoded_data, headers={'Authorization': 'Bearer ' + access_token})


data = json.loads(r.data.decode('utf-8'))
print(data['name'])
print(data['description'])

Nick Sciarrilli

unread,
Mar 1, 2021, 1:56:34 PM3/1/21
to Bryant Likes, Strava API
I figured it out. I needed to include the 'Content-Type': 'application/json'. FML such a rookie mistake. Not sure why the swagger client in python is not working. You would think that would be included.

Bryant Likes

unread,
Mar 1, 2021, 1:57:52 PM3/1/21
to Nick Sciarrilli, Strava API
So if you compare the output on that with what you use in postman, is there no difference? 

I've never actually used the API to update an activity, but are the fields you're not passing optional? I'm assuming they are. 


Nick Sciarrilli

unread,
Mar 1, 2021, 2:00:55 PM3/1/21
to Bryant Likes, Strava API
Yeah the update fields are optional. You only need to pass the one you want to update.

This is the python which is working:

#!/usr/bin/env python3
import json
import urllib3

http = urllib3.PoolManager()

activity_id = '4872907378'
access_token = '###'
auth = 'Bearer ' + access_token

body = {'name': 'daft punk', 'description': "ilikespicy.com"}
encoded_data = json.dumps(body)

headers = {'Authorization': auth, 'Content-Type': 'application/json'}


r = http.urlopen(
'PUT',
url,
headers=headers,
body=encoded_data,
)


data = json.loads(r.data.decode('utf-8'))
print(data['name'])
print(data['description'])
Reply all
Reply to author
Forward
0 new messages