我该如何在Python中处理这个HTTPS请求?

问题描述 投票:2回答:3

我试图在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知之甚少

更新:根据Merlin建议使用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)
python https urllib getaddrinfo
3个回答
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)

玩得开心!


3
投票

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


1
投票

你需要使用httplib。到达HTTPS服务器的示例代码:

import httplib

con = httplib.HTTPSConnection('www.google.com')
con.request("GET", "/")
res = con.getresponse()
print res.read()
© www.soinside.com 2019 - 2024. All rights reserved.