Reputation: 27605
I am trying to use the Strava API v3 in Python, and I am afraid I am missing something. The docs say:
This base URL is used for all Strava API requests: https://api.strava.com
$ curl -i https://api.strava.com
HTTP/1.1 200 OK Content-Type: application/json Status: 200 OK X-RateLimit-Limit: 5000 X-RateLimit-Remaining: 4999 Content-Length: 2
Responses are in JSON format and gzipped.
I am currently doing this:
import urllib
print urllib.urlopen('https://api.strava.com').read()
And gettin this:
Traceback (most recent call last):
File "StravaAPIv3.py", line 3, in <module>
print urllib.urlopen('https://api.strava.com').read()
File "C:\Python27\lib\urllib.py", line 86, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 207, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 436, in open_https
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 814, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 776, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 1157, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno 11004] getaddrinfo failed
I don't know where to start, since I don't know much about HTTP requests and HTTPS
UPDATE: According to Merlin's suggestion to use requests
module, I am doing this:
import requests
r = requests.get('https://api.strava.com/')
print r.status_code
print r.headers['content-type']
print r.encoding
print r.text
print r.json()
but keep getting an error:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.strava.com', port=443): Max retries exceeded with url: / (Caused by <class 'so cket.gaierror'>: [Errno 11004] getaddrinfo failed)
Upvotes: 2
Views: 4116
Reputation: 385
You need to follow the instructions here first: http://strava.github.io/api/v3/oauth/ Basically, the app you create has to authorize the user using it (in this case, you). I've written some example code below. I'm new to python so don't know how to automate log in, so you'll have to copy and paste the url to the browser and copy and paste the code in.
import requests
import json
#Replace #### with your client id (listed here: http://www.strava.com/settings/api)
#Replace &&&& with your redirect uri listed on the same page. I used localhost
#Go this url in your browser and log in. Click authorize
https://www.strava.com/oauth/authorize?client_id=###&response_type=code&redirect_uri=&&&&
#Copy and paste the code returned in the url
code='qwertyuio123456789'
#Replace @@@@ with the code on your api page
values={'client_id':'###', 'client_secret': '@@@@', 'code':code}
r = requests.post('https://www.strava.com/oauth/token', data=values)
json_string = r.text.replace("'", "\"")
values = json.loads(json_string)
#now you have an access token
r = requests.get('http://www.strava.com/api/v3/athletes/227615', params=values)
Have fun!
Upvotes: 2
Reputation: 3952
You need to use httplib. Sample code to get to a HTTPS server:
import httplib
con = httplib.HTTPSConnection('www.google.com')
con.request("GET", "/")
res = con.getresponse()
print res.read()
Upvotes: 1
Reputation: 25659
Try using requests! It's safer. http://docs.python-requests.org/en/latest/
Upvotes: 3