Microsoft 访问令牌请求错误 - 400 错误请求

Microsoft Access Token Request Error - 400 Bad Request

我正在尝试从访问代码获取访问令牌。但是出现了400 Bad Request error

的错误

我正在使用此 link 获取访问代码

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&response_type=code&redirect_uri=http://localhost/Home/About&scope=mail.read

作为对此 link 的响应,我在查询字符串中获取代码 return url 代码=Mh12d04c8-8ea9-c82b-8cc5-b93cf913382c

我正在使用此代码获取访问令牌

https://login.microsoftonline.com/common/oauth2/v2.0/token?redirect_uri=http://localhost:51296/Home/AAA&client_id=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&client_secret=Rmi2PNLPNM5agLh5aT5G82W&code=Mh12d04c8-8ea9-c82b-8cc5-b93cf913382c&scope=mail.read

但是它给出了 400 Bad Request。请帮我解决这个问题。

对于这种问题,正如@RasmusW 所建议的,您可以使用像 Fiddler 这样的工具来查找响应中的内部错误消息。正如@Dan Kershaw 指出的那样,您需要确保令牌请求中的回复地址 url 应与授权代码请求中的回复地址相匹配。否则您将收到错误消息:回复地址 'xxxxx' 与请求授权码时提供的回复地址 'xxxx' 不匹配。

根据您的要求,您正在使用带有 v2.0 端点的授权代码流。您必须在令牌请求中添加一个“grant_type”参数,并且值必须是“authorization_code”授权代码流。请先查看内部报错信息是否为“AADSTS90014: The request body must contain the following parameter: 'grant_type”。请单击 here 了解有关 v2.0 端点中 OAuth 2.0 授权代码流的更多详细信息。

May i use personal Microsoft account with Microsoft Graph API

是。v2.0 端点允许开发人员使用单个身份验证端点编写接受来自 Microsoft 帐户和 Azure AD 帐户的 sign-in 的应用程序。