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

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 に答える