Django OAuth Toolkit-使用访问令牌进行身份验证

时间:2020-05-12 15:47:25

标签: python django oauth oauth-2.0 django-oauth

我正在尝试遵循本教程中的步骤: https://django-oauth-toolkit.readthedocs.io/en/1.1.2/tutorial/tutorial_03.html

本教程显示,如果我们在授权标头中包含访问令牌,则可以对用户进行身份验证。

curl -H "Authorization: Bearer 123456" -X GET http://localhost:8000/secret

但是,似乎没有显示如何在Django循环中包括Authorization标头。 (不使用curl,仅在用户使用提供程序登录后)我觉得在回调之后需要有一个视图来注册/验证用户。

我有一个提供程序,正在向我的callback_url返回授权令牌。 之后,我使用以下视图通过对服务器的POST请求将其交换为访问令牌。 它返回的响应包含可用的用户名,访问令牌,有效日期和更多信息。

我应如何继续使用此数据对用户进行身份验证?

是否有必要创建一个用户,或者如何将此令牌添加到经过身份验证的令牌列表中并返回用户的授权标头?

import requests

def authdt(request):
    authentication_code = XYZ123
    url = "https://PROVIDER/api/v3/oauth/token"
    payload = authentication_code
    headers = {
        'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
        'cache-control': "no-cache",
    }

    response = requests.request("POST", url, data=payload, headers=headers)
    # How can I use response to create/authorize user?
    render(request, "index.html")

本教程提到使用OAuth2身份验证后端。 https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/backends.py

authorize方法将请求对象作为输入。 我不了解如何连接一切。

0 个答案:

没有答案