【问题标题】:Python access to API- Authentication ErrorPython访问API-Authentication Error
【发布时间】:2022-12-09 19:57:37
【问题描述】:

我一直在使用以下命令通过 curl 访问 supportpal API。 (https://docs.supportpal.com/current/REST+API)

curl.exe -i -u 'APIKEY:x' -X GET https://support.url.org/api/user/user/3697

这正确地抓取了数据。我试图用 python 复制它,但我不断遇到身份验证问题并收到以下错误。

Failed to authenticate because of bad credentials or an invalid authorization header

我使用的代码很简单。

import requests
import json

url = "https://support.url.org/api/user/user/3697"

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer: {APIKEY:x}"
}

response = requests.request("GET", url, headers=headers)
print(response. Text)

我在想我的 auth 标头有问题,但无法弄清楚。

【问题讨论】:

    标签: python api python-requests


    【解决方案1】:
    from requests.auth import HTTPBasicAuth
    import requests
    
    url = 'https://support.url.org/api/user/user/3697'
    headers = {'Accept': 'application/json'}
    auth = HTTPBasicAuth('apikey', x)
    
    req = requests.get(url, headers=headers, auth=auth)
    

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2023-01-18
      • 2015-01-13
      • 1970-01-01
      • 2018-05-22
      相关资源
      最近更新 更多