編輯 - 因爲我無法Strava這個標籤這裏是文檔,如果你有興趣 - http://strava.github.io/api/Get請求活動strava V3 API的Python
我通過認證得很好,並獲得的access_token(和我運動員信息)在一個response.read。
我在下一步遇到問題: 我想返回有關特定活動的信息。
import urllib2
import urllib
access_token = str(tp[3]) #this comes from the response not shown
print access_token
ath_url = 'https://www.strava.com/api/v3/activities/108838256'
ath_val = values={'access_token':access_token}
ath_data = urllib.urlencode (ath_val)
ath_req = urllib2.Request(ath_url, ath_data)
ath_response = urllib2.urlopen(ath_req)
the_page = ath_response.read()
print the_page
誤差
Traceback (most recent call last):
File "C:\Users\JordanR\Python2.6\documents\strava\auth.py", line 30, in <module>
ath_response = urllib2.urlopen(ath_req)
File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 389, in open
response = meth(req, response)
File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 502, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 427, in error
return self._call_chain(*args)
File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 361, in _call_chain
result = func(*args)
File "C:\Users\JordanR\Python2.6\lib\urllib2.py", line 510, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
404是一個謎,因爲我知道這個活動的存在呢?
'access_token'是否正確的標題信息? 該文檔(http://strava.github.io/api/v3/activities/#get-details)使用授權:承載?我不確定liburl如何編碼信息的承載部分?
對不起,如果我的一些術語有點偏離,我是新手。
回答了這個壞男孩。
import requests as r
access_token = tp[3]
ath_url = 'https://www.strava.com/api/v3/activities/108838256'
header = {'Authorization': 'Bearer 4b1d12006c51b685fd1a260490_example_jklfds'}
data = r.get(ath_url, headers=header).json()
它需要在「詞典」中添加「承載」部分。
感謝您的幫助idClark
我也無法理解承載參數。你有沒有嘗試從命令行擊中端點?如果我做'curl -XGET https://www.strava.com/api/v3/activities/111008284 -H「授權:持證人my_access_token_goes_here」| jq'。''我可以找回JSON就好了。稍後我會嘗試使用Python。 – idclark