【问题标题】:How to authorize via token in Python to an API如何通过 Python 中的令牌向 API 授权
【发布时间】:2019-12-18 17:22:57
【问题描述】:

我是 Python 编程的新手,我正在尝试使用电动滑板车租赁服务并从滑板车上抓取数据。服务提供商是 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 中的令牌作为正确的授权...... 很抱歉打扰你,但我真的不知道如何进步。

非常感谢

【问题讨论】:

    标签: python web-crawler authorization http-status-code-401


    【解决方案1】:

    我认为您使用的令牌不正确。在我看来,您正在使用 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 &lt;ACCESS-TOKEN&gt;

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2018-06-13
      • 2014-06-30
      • 2020-04-27
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2020-12-22
      • 2013-04-11
      相关资源
      最近更新 更多