如何将 Lyft api 中的以下 curl 命令转换为 http 接口(interface)请求(以便它们可以像 https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY 一样通过网络执行)?如果无法进行 http 请求翻译,我将如何在 R 中集成和处理这些 curl 命令?
#Authentication code
curl -X POST -H "Content-Type: application/json" \
--user "<client_id>:<client_secret>" \
-d '{"grant_type": "client_credentials", "scope": "public"}' \
'https://api.lyft.com/oauth/token'
#Search query
curl --include -X GET -H 'Authorization: Bearer <access_token>' \
'https://api.lyft.com/v1/eta?lat=37.7833&lng=-122.4167'
最佳答案
您好,您可以使用 https://curl.trillworks.com/将 curl 命令转换为您选择的语言,或者您可以使用 lyft SDK(对于 Python,请使用 https://pypi.python.org/pypi/lyft_rides)。
这是对应的Python版本
import requests
headers = {
'Content-Type': 'application/json',
}
data = '{"grant_type": "client_credentials", "scope": "public"}'
requests.post('https://api.lyft.com/oauth/token', headers=headers, data=data, auth=('<client_id>', '<client_secret>'))
从此发布请求中,您将获得必须用于后续请求的访问 token 。
headers = {
'Authorization': 'Bearer <access_token>',
}
requests.get('https://api.lyft.com/v1/eta?lat=37.7833&lng=-122.4167', headers=headers)
注意:我没有对此进行测试,因为我无法创建 lyft 开发者帐户,因此此处给出的代码可能会有一些细微的变化。
关于r - 将 curl 命令转换为 http 请求接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809708/