Python,具有基本身份验证的 HTTPS GET,然后是基本身份验证后的令牌

时间:2021-07-13 13:24:19

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

我真的需要你的帮助。我正在尝试创建一个 python 程序,以使用 API 定期下载服务器内容 (JSON),然后将其呈现给前端。我目前使用 Postman 从供应商服务器访问我想要的数据。要访问数据,我必须使用基本身份验证用户名和密码登录,服务器使用令牌响应,然后我将使用另一个不记名令牌授权方法,使用给定的令牌作为成功登录的输入。< /p>

如何在我的 python 环境中实现这一点?

下面的代码返回 401。

'''

def GetDroplets(request):
    username = "myemail@email.com"
    password = "MyPassword202"
    url = "https://vrmapi.victronenergy.com/v2/auth/login"
    token = '243hfhwwccw83748738475843jn8jmjwjfwjfwcwcwmijwfe84jfew'
    headers = {'X-Authorization':token}
    r = requests.post(url, auth=(username, password), headers=headers)
    
    print(r.status_code)  
    print(type(r))
    context = {'r': r}
    
    return render (request, 'droplets.html', context)

'''

0 个答案:

没有答案