我正在使用AWS Cognito UI进行授权码授权流程的登录,并成功获取了授权码。但是当通过Postman向oauth2/token端点发出POST请求时,收到了405方法不允许的错误。
应用程序客户端在Cognito用户池中进行设置,并将appclientid:appclientsecret作为授权以base64编码方式传递。
我正在使用AWS Cognito UI进行授权码授权流程的登录,并成功获取了授权码。但是当通过Postman向oauth2/token端点发出POST请求时,收到了405方法不允许的错误。
应用程序客户端在Cognito用户池中进行设置,并将appclientid:appclientsecret作为授权以base64编码方式传递。
根据文档所述:
Content-Type 必须始终为 'application/x-www-form-urlencoded'。
来源:https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html
使用身份验证的BasicAuth,并提供 Username
=client_id
, Password
=client_secret
使用 POST
方法
使用 Body
= x-www-form-urlencoded
不要忘记在 Body 中使用 State
值。
我曾经遇到过类似的问题。在我的情况下,我需要将“Accept”标头更改为*/*
。
当我将其设置为Accept=text/html,application/xhtml+xml,application/xml
时,它会对/token端点响应405。希望这能帮助某些人。
我在为授权码授权类型编写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
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
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"
}
{
"client_id": {client_id},
"grant_type": "client_credentials",
"scope": {client_scope}
}
然而,我仍然遇到了 405: 方法不允许
的错误。最终我发现可以在Postman中将body类型切换为x-www-form-urlencoded
,重新输入body参数,现在已经可以工作了。
希望对任何人有所帮助。
在尝试使用授权码作为 grant_type 时,我在 Postman 中遇到了 405 错误,因为我在头部部分使用了
'application/x-www-form-urlencoded'
作为Content-Type
的值,即带有单引号。当我删除这些单引号,直接使用application/x-www-form-urlencoded
时,它开始正常工作。
v0.x.x
升级到v1.x.x
后,我改变了
headers: {'content-type': 'application/x-www-form-urlencoded'},
改为
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
在Content-Type
中使用大写字母。 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);