繁体   English   中英

如何通过 Python 中的令牌向 API 授权

[英]How to authorize via token in Python to an API

我完全是 Python 编程的新手,我正在尝试访问 ae – 踏板车租赁服务并抓取踏板车的数据。 服务提供者是 Bird,没有官方 API,只能通过 Android 或 iOS 应用程序访问。 有人找到了一种方法,这里记录了对帖子和获取的要求: https : //github.com/ubahnverleih/WoBike/blob/master/Bird.md

我的问题是在我通过令牌授权自己之后,我无法创建获取请求。 服务器一直说:{"code":401,"message":"访问此资源需要凭据"}

这是我的代码:

import requests
import json
import uuid
target = "https://api-auth.prod.birdapp.com/api/v1/auth/email"
headers = { "User-Agent": "Bird/4.53.0 (co.bird.Ride; build:24; iOS 12.4.1) Alamofire/4.53.0"
           ,"Platform": "ios",
           "App-Version": "4.53.0",
           "Content-Type": "application/json",
           "Device-Id": str(uuid.uuid4())}
data = {"email": "any@mail.com"}
reply = requests.post(target, data=json.dumps(data), headers=headers)

# at this point u get an email and u need to copy the token in the email

token = {"token":"IGzMtdAkQ3icmSFV0V64yQ"}
url_token = 'https://api-auth.prod.birdapp.com/api/v1/auth/magic-link/use'

# now you are authorized and you can start get-requests

get_headers = {"Authorization" : "IGzMtdAkQ3icmSFV0V64yQ",
              "Device-id" : str(uuid.uuid4()),
              "App-Version" : "4.41.0",
              "Location" : json.dumps({"latitude":37.77249,"longitude":-122.40910,"altitude":500,"accuracy":100,"speed":-1,"heading":-1})}
url_get_birds = "https://api.birdapp.com/bird/nearby?latitude=37.77184&longitude=-122.40910&radius=1000"
print((requests.get(url_get_birds, headers = get_headers)).text)

最后一个打印命令一直给我 401 错误

我想我不使用 get_headers 中的令牌作为正确的授权......很抱歉打扰你,但我真的不知道如何进行。

非常感谢

我认为您没有使用正确的令牌。 在我看来,您正在使用 Magic Link 令牌作为获取滑板车位置的身份验证。 您的电子邮件中包含的 Magic Link 令牌不能用作获取滑板车位置的令牌,它用作获取您的访问令牌的令牌。 正确的标记应该以ey和一长串字符开头。

尝试运行它以获取您的实际令牌:

curl --location --request POST 'https://api-auth.prod.birdapp.com/api/v1/auth/magic-link/use' \
--header 'User-Agent: Bird/4.53.0 (co.bird.Ride; build:24; iOS 12.4.1) Alamofire/4.53.0' \
--header 'Device-Id: <YOUR-UUID>' \
--header 'Platform: ios' \
--header 'App-Version: 4.53.0' \
--header 'Content-Type: application/json' \
--header 'Content-Type: text/plain' \
--data-raw '{"token":"9BntNB548R7KyD42ml4hrJA"}'

这将输出 2 种令牌 - 访问和刷新。 访问令牌是您用来获取滑板车位置的令牌。 它应该像这样实现: Authorization: Bird <ACCESS-TOKEN>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM