【问题标题】:Get request Activities for strava v3 api python获取 strava v3 api python 的请求活动
【发布时间】:2014-01-29 09:31:13
【问题描述】:

编辑 - 因为我无法用 Strava 标记它,如果你有兴趣,这里是文档 - http://strava.github.io/api/

我顺利通过了身份验证,并在 response.read 中获得了 access_token(以及我的运动员信息)。

我在执行下一步时遇到问题: 我想返回有关特定活动的信息。

    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) 使用 Authorization: Bearer ?我不确定 liburl 如何对信息的 Bearer 部分进行编码?

对不起,如果我的一些术语有点不对劲,我是新手。

回答了这个坏男孩。

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 的帮助

【问题讨论】:

  • 我也无法理解 Bearer 参数。您是否尝试过从命令行点击端点?如果我这样做 curl -XGET https://www.strava.com/api/v3/activities/111008284 -H "Authorization: Bearer my_access_token_goes_here" | jq '.' 我可以取回 JSON 就好了。稍后我会尝试使用 Python。

标签: python api urllib2 urllib


【解决方案1】:

我更喜欢使用第三方Requests 模块。您确实需要遵循文档并使用 Authorization: Header 记录在the API

请求has an arg for header data。然后我们可以创建一个字典,其中键为Authorization,其值为单个字符串Bearer access_token

#install requests from pip if you want
import requests as r
url = 'https://www.strava.com/api/v3/activities/108838256'
header = {'Authorization': 'Bearer access_token'}
r.get(url, headers=header).json()

如果你真的想使用 urllib2

#using urllib2
import urllib2
req = urllib.Request(url)
req.add_header('Authorization', 'Bearer access_token')
resp = urllib2.urlopen(req)
content = resp.read()

请记住access_token 需要是文字字符串值,例如 acc09cds09c097d9c097v9

【讨论】:

  • 感谢@idclark 看这个。我尝试过请求但失败了,我确实得到了一个 json 响应,所以有些成功,尽管它告诉我授权失败。这是一个愚蠢的问题吗,标题行上的“Bearer access_token”应该是我的文字字符串值吗?
  • 修改了问题。
猜你喜欢
  • 2020-10-31
  • 2022-07-31
  • 2019-03-23
  • 2017-04-22
  • 2020-07-25
  • 2021-03-15
  • 2021-09-28
  • 2020-12-27
  • 2023-02-01
相关资源
最近更新 更多