AWS Cognito oauth2/token 端点出现 405 方法不被允许的错误

26

我正在使用AWS Cognito UI进行授权码授权流程的登录,并成功获取了授权码。但是当通过Postman向oauth2/token端点发出POST请求时,收到了405方法不允许的错误。

应用程序客户端在Cognito用户池中进行设置,并将appclientid:appclientsecret作为授权以base64编码方式传递。

10个回答

25

有时候需要有人用粗体字声明一下:"application/x-www-form-url-encoded" 不等同于 "application/x-www-form-urlencoded" ... 谢谢。我在这个问题上自己给自己挖了个坑。 - Josh Peak

9

使用身份验证的BasicAuth,并提供 Username=client_id, Password=client_secret

使用 POST 方法

使用 Body = x-www-form-urlencoded

不要忘记在 Body 中使用 State 值。


4

我曾经遇到过类似的问题。在我的情况下,我需要将“Accept”标头更改为*/*

当我将其设置为Accept=text/html,application/xhtml+xml,application/xml时,它会对/token端点响应405。希望这能帮助某些人。


4

我在为授权码授权类型编写c#代码时,所有调用均以405方法不允许状态失败。

根据AWS文档,应使用以下URL和参数:

POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token&
Content-Type='application/x-www-form-urlencoded'&
Authorization=Basic aSdxd892iujendek328uedj

grant_type=authorization_code&
client_id=djc98u3jiedmi283eu928&
code=AUTHORIZATION_CODE&
redirect_uri=com.myclientapp://myclient/redirect

在花费了两个小时的时间后,我发现从URL中移除&可以解决问题,因此请确保您的请求看起来像这样。
POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token
Content-Type='application/x-www-form-urlencoded'
Authorization=Basic aSdxd892iujendek328uedj

grant_type=authorization_code&
client_id=djc98u3jiedmi283eu928&
code=AUTHORIZATION_CODE&
redirect_uri=com.myclientapp://myclient/redirect

3
我通过以下代码解决了AWS Cognito oauth2/token端点的405方法不允许错误,并且它可以正常工作。 我从这个链接中获得帮助,并使用正确的格式在fetch请求中提及头部和主体参数:

https://formcarry.com/documentation/fetch-api-example

  const requestOptions = {
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": `Basic ${authData}`,
      "Accept": "application/json"            
    },
    body: `grant_type=${config.grant_type}&code=${code}&client_id=${config.clientId}&redirect_uri=${config.loginRedirectUri}`
  }
        
  fetch(`${config.domainUrl}/oauth2/token`, requestOptions)
    .then(response => response.json())
    .then(data => {
      sessionStorage.setItem("access_token",data.access_token)
      fetchUserDetails(data.access_token)
    })

我使用配置文件保存变量。
const config = {
  domainUrl: "https://domainname.auth.origin.amazoncognito.com",
  clientId: "xxxxxxxxxxxx",
  loginRedirectUri: "http://localhost:8000/redirecturi",
  grant_type: "authorization_code",
  logoutUri: "http://localhost:8000",
  clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

1
我曾经遇到过同样的问题,不过是在使用client_credentials而非authorization_code时。在Postman中,我使用了有效的client_id/client_secret作为用户名/密码进行基本身份验证,并确保Content-Type头部设置为application/x-www-form-urlencoded。然后将请求体(raw/json)设置为:
{
"client_id": {client_id},
"grant_type": "client_credentials",
"scope": {client_scope}
}

然而,我仍然遇到了 405: 方法不允许 的错误。最终我发现可以在Postman中将body类型切换为x-www-form-urlencoded,重新输入body参数,现在已经可以工作了。


1

希望对任何人有所帮助。

在尝试使用授权码作为 grant_type 时,我在 Postman 中遇到了 405 错误,因为我在头部部分使用了 'application/x-www-form-urlencoded' 作为 Content-Type 的值,即带有单引号。当我删除这些单引号,直接使用 application/x-www-form-urlencoded 时,它开始正常工作。


1
这正是解决我在Cognito遇到的问题的方法,我一直收到相同的405错误。谢谢@Prateek! - Will Bellman

0
在我的情况下,将axiosv0.x.x升级到v1.x.x后,我改变了 headers: {'content-type': 'application/x-www-form-urlencoded'}, 改为 headers: {'Content-Type': 'application/x-www-form-urlencoded'},Content-Type中使用大写字母。

0
为了让画面更完整,如果您的主机头未设置或与您要发布到的域不同,则还会收到405(方法不允许)的错误提示。

0
        var strClientSecret = $"{"your_clientId"}:{"your_clientsecret"}";
        var client = new HttpClient();
        var body = new Dictionary<string, string>();
        body.Add("grant_type", "client_credentials");
        body.Add("client_id", "your_appclientid");
        body.Add("redirect_uri", "your_callbackurl");

        var content = new FormUrlEncodedContent(body);
        var autho = System.Text.Encoding.UTF8.GetBytes(strClientSecret);
        var base64Autho = System.Convert.ToBase64String(autho);
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Autho);

        client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");

        var response = await client.PostAsync("https://your_domain.auth.ap-south-1.amazoncognito.com/oauth2/token", content);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接