How should I handle this HTTPS request in Python? -
i trying use strava api v3 in python, , afraid missing something. docs say:
this base url used 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 in json format , gzipped.
i 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 res in getaddrinfo(host, port, 0, sock_stream): ioerror: [errno socket error] [errno 11004] getaddrinfo failed
i don't know start, since don't know http requests , https
update: according merlin's suggestion use requests
module, 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 error: requests.exceptions.connectionerror: httpsconnectionpool(host='api.strava.com', port=443): max retries exceeded url: / (caused <class 'so cket.gaierror'>: [errno 11004] getaddrinfo failed)
you need follow instructions here first: http://strava.github.io/api/v3/oauth/ basically, app create has authorize user using (in case, you). i've written example code below. i'm new python don't know how automate log in, you'll have copy , paste url browser , copy , paste code in.
import requests import json #replace #### client id (listed here: http://www.strava.com/settings/api) #replace &&&& redirect uri listed on same page. used localhost #go url in browser , log in. click authorize https://www.strava.com/oauth/authorize?client_id=###&response_type=code&redirect_uri=&&&& #copy , paste code returned in url code='qwertyuio123456789' #replace @@@@ code on 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 have access token r = requests.get('http://www.strava.com/api/v3/athletes/227615', params=values)
have fun!
Comments
Post a Comment