首页 > 解决方案 > 错误:来自 django oauth2 服务器的“unsupported_grant_type”

问题描述

不知道为什么 django 不接受我对访问令牌的 POST 请求。我所有的参数都是正确的,并且我已经有了授权码,但是访问令牌的后续 POST 请求给了我这个错误。

从我从其他人那里读到的内容类型是正确的。如果 pkce 方面不准确,它会给我一个更具体的错误。

HttpErrorResponse 是 { error: "unsupported_grant_type" } 400 bad request

 requestToken(code: string, state: string) {
  const clientState = sessionStorage.getItem('pkce-state');
   if (clientState !== state) {
    console.error('States do not match!');
   }

  const verifier = sessionStorage.getItem('pkce-verifier');

  const params = new URLSearchParams({
   grant_type: 'authorization_code',
   redirect_uri: 'http://localhost:4200/dashboard',
   client_id: 'client_id',
   code,
   state,
   verifier
 });

return this.http.post('http://localhost:8000/o/token/',
  {
    params
  },
  {
    withCredentials: true,
    headers: new HttpHeaders({
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    )
  });

}

也试过这个:

requestToken(code: string, state: string) {
  const clientState = sessionStorage.getItem('pkce-state');
   if (clientState !== state) {
     console.error('States do not match!');
   }
  const verifier = sessionStorage.getItem('pkce-verifier');

  return this.http.post('http://localhost:8000/o/token/',
    {
      grant_type: 'authorization_code',
      redirect_uri: 'http://localhost:4200/dashboard',
      client_id: 'client_id',
      code,
      state,
      verifier
    },
    {
      withCredentials: true,
      headers: new HttpHeaders({
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      }
    )
  });

}

标签: djangoangularoauth-2.0

解决方案


尝试精确匹配我写的第 7 步

  • 使用 code_verifier
  • 去掉 state 参数,直接 https 消息到令牌端点时不需要该参数

错误消息通常具有误导性,但这将使您的消息 100% 符合标准,并希望能够奏效。

再说一次,Django总是有可能不正确支持这个流程......


推荐阅读