获取strava v3 api python的请求活动

2024-06-08 09:00:58 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑-因为我不能用Strava标记这个,如果您感兴趣,这里是文档-http://strava.github.io/api/

我通过了认证罚款,并在响应。读取. 在

我对下一步有问题: 我要返回有关特定活动的信息。在

    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

错误是

^{pr2}$

404是一个谜,因为我知道这个活动存在?在

“access_token”是正确的头信息吗? 文档(http://strava.github.io/api/v3/activities/#get-details)使用Authorization:Bearer?我不确定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()

它需要在口述中加上“承载”部分

谢谢你的帮助艾德克拉克


Tags: the文档importtokenapi信息urldata
1条回答
网友
1楼 · 发布于 2024-06-08 09:00:58

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

请求has an arg for header data。然后我们可以创建一个dict,其中键是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

^{pr2}$

请记住access_token必须是文本字符串值,例如acc09cds09c097d9c097v9

相关问题 更多 >