2

我正在尝试在 Python 中使用Strava API v3,但恐怕我遗漏了一些东西。文档说:

此基本 URL 用于所有 Strava API 请求: 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

响应采用 JSON 格式并经过 gzip 压缩。

我目前正在这样做:

import urllib
print urllib.urlopen('https://api.strava.com').read()

并开始这样做:

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

我不知道从哪里开始,因为我不太了解 HTTP 请求和 HTTPS

更新:根据梅林使用requests模块的建议,我这样做:

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() 

但不断收到错误:

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)
4

3 回答 3

3

尝试使用请求!它更安全。 http://docs.python-requests.org/en/latest/

于 2013-05-09T12:44:28.087 回答
2

您需要先按照此处的说明进行操作:http: //strava.github.io/api/v3/oauth/ 基本上,您创建的应用程序必须授权用户使用它(在这种情况下,您)。我在下面写了一些示例代码。我是python新手,所以不知道如何自动登录,所以你必须将url复制并粘贴到浏览器中,然后复制并粘贴代码。

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)

玩得开心!

于 2013-10-20T22:29:13.300 回答
1

您需要使用httplib。访问 HTTPS 服务器的示例代码:

import httplib

con = httplib.HTTPSConnection('www.google.com')
con.request("GET", "/")
res = con.getresponse()
print res.read()
于 2013-05-09T12:48:46.497 回答