【问题标题】:Django OAuth2 unsupported_grant_typeDjango OAuth2 unsupported_grant_type
【发布时间】:2017-08-29 07:53:56
【问题描述】:

我正在尝试使用 django 发送请求,以使用 OAuth2 从我的 api 获取 access_token。我正在执行这段代码:

data = {'username': 'admin', 'password': '123123', 'grant_type': 
'password','client_id': 'xxx','client_secret': 'xxx'}
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, data=data, headers=headers)

当我发送此请求时,我收到此错误:

{'error': 'unsupported_grant_type'}

感谢您的帮助!

【问题讨论】:

    标签: django oauth-2.0


    【解决方案1】:

    如果有人感兴趣,正确的请求是:

        payload = "grant_type=password&client_secret=xxx&client_id=xxx&username=username&password=password"
        headers = {
            'content-type': "application/x-www-form-urlencoded",
            'cache-control': "no-cache",
        }
    
        response = requests.request("POST", url, data=payload, headers=headers)
    

    【讨论】:

    • 感谢您的回答。我使用 application/json 作为内容类型。切换到 x-www-form-urlencoded 是我需要更改的所有内容,以使其适用于 client_credentials 授权类型。
    【解决方案2】:

    如果你不想在 url 中编码数据,你可以把它放在你的设置中。

    OAUTH2_PROVIDER = {                                                                                                                                                                      
        'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore',                                                                                                          
    }
    

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题,后来发现授权类型应该是字符串。

      payload = {'grant_type': 'refresh_token', 'refresh_token': refresh_token}
      

      【讨论】:

      • 如果你能解释一下答案就好了。欢迎加入社区。​​span>
      • 授权类型应输入为字符串值。 (在单引号内)。非常感谢您的反馈。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      相关资源
      最近更新 更多