api上的请求无效(Curl和python)

时间:2018-11-18 16:37:35

标签: python python-3.x api curl python-requests

因此,我尝试编写一个程序来告诉我公交车的时间表,我将这些实时信息存储在Api上:

https://api-lab-trone-stif.opendata.stif.info/service/tr-vianavigo/departures?line_id=100100385%3A385&stop_point_id=StopPoint%3A59%3A6195786

但是当我午餐时有一个错误:

"error": "Unknown dataset: tr-vianavigo"

我认为我可以使用令牌,但是怎么用? 该API的Curl为:

curl -X GET --header "Accept: application/json" "https://api-lab-trone- 
stif.opendata.stif.info/service/tr-vianavigo/departures? 
line_id=100100020%3A20&stop_point_id=59%3A4726914"

当我在网络浏览器中使用第一个URL时,它会工作并显示给我:

[
  {
    "lineDirection": "Juvisy RER",
    "code": "duration",
    "time": "12"
  },
  {
    "lineDirection": "Savigny Toulouse-Lautrec",
    "code": "duration",
    "time": "20"
  },
  {
    "lineDirection": "Juvisy RER",
    "code": "duration",
    "time": "38"
  },
  {
    "lineDirection": "Savigny Toulouse-Lautrec",
    "code": "duration",
    "time": "45"
  }
]

但是如果我只是在python中测试:

import requests 

#my token
apikey = 'ef169369dfd911b01371b73e975ed88adce881bc92dbbd9720551c94'

#url of the api
url ='https://api-lab-trone-stif.opendata.stif.info/service/tr-vianavigo/departures?line_id=100100385%3A385&stop_point_id=StopPoint%3A59%3A6195786'

#requests
r = requests.get(url, apikey)

print(r.text)

它会导致错误:

    "error": "Unknown dataset: tr-vianavigo"

1 个答案:

答案 0 :(得分:0)

为此,您必须使用您的API密钥之一。您可以通过My API keys中的用户帐户获取API密钥。密钥生成后,必须作为查询参数(apikey)插入到您的网址中 呼叫。例如:

curl -X GET --header "Accept: application/json" "https://api-lab-trone-stif.opendata.stif.info/service/tr-vianavigo/departures?line_id=100100020%3A20&stop_point_id=59%3A4726914&apikey=<YOUR-API-KEY>"

仅供参考,数据集tr-unitaire-stif上可用的实时数据范围是here