【问题标题】:oauth2 error AADSTS90014: The request body must contain the following parameter: 'grant_type'oauth2 错误 AADSTS90014:请求正文必须包含以下参数:“grant_type”
【发布时间】:2018-09-05 21:02:10
【问题描述】:

在 Windev 的开发中,我使用 Oauth 2.0 进行授权,以从用户那里获得对 Outlook 邮件的访问权限。

应用程序在https://apps.dev.microsoft.com 注册,没有隐式工作流。 用户输入凭据后,将返回授权码。 使用新代码,承载令牌是通过 HTTP Post 命令请求的。

到目前为止,一切都很好。

只是响应给出了一条对我来说毫无意义的错误消息。

在代码中:

m_sHTTPUrl = "client_id=" + m_sClientID + "&client_secret=" ...
    + m_sClientSecret ...
    + "&redirect_uri=" + m_sRedirectURL + "&code=" + m_sAuthToken ...
    + "&grant_type=authorization_code"
m_sHTTPres = ""
LogLocalFile("GetAccessToken - " + m_sTokenURL + " // " + m_sHTTPUrl) 

cMyRequest is httpRequest
cMyRequest..Method = httpPost
cMyRequest..URL = m_sTokenURL
cMyRequest..ContentType = "application/x-www-form-urlencoded"
cMyRequest..Header["grant_type"] = "authorization_code"
cMyRequest..Header["code"] = m_sAuthToken
cMyRequest..Header["client_id"] = m_sClientID
cMyRequest..Header["client_secret"] = m_sClientSecret
cMyRequest..Header["scope"] = m_sScope
cMyRequest..Header["redirect_uri"] = m_sRedirectURL
//cMyRequest..Content = m_sHTTPUrl
cMyResponse is httpResponse = HTTPSend(cMyRequest)
m_sHTTPres = cMyResponse.Content

在一个日志文件中,我请求了使用的参数和 httpResponse 的内容:

GetAccessToken - https://login.microsoftonline.com/common/oauth2/v2.0/token // grant_type=authorization_code
&code=xxxxxxx
&scope=openid+offline_access+User.Read+Email+Mail.Read+Contacts.Read
&redirect_uri=http://localhost/
&client_id=xxxxxxx
&client_secret=xxxxxxx

GetAccessToken - error = invalid_request
GetAccessToken - error_description = AADSTS90014: The request body must contain the following parameter: 'grant_type'.

grant_type 应该在标头中。

有没有人知道需要什么才能让 OAUTH2 正常工作?

【问题讨论】:

  • 据此post oauth-2.0 参数必须在您的请求内容中。你已经试过了吗?这个post 也警告正文的编码。
  • 感谢您的指导。 a) 它必须在正文中,而不是在标题中。 b) 它必须以纯文本形式进行编码。比它有效。

标签: http oauth-2.0 bearer-token windev


【解决方案1】:

您不应该在参数或标题中发送grant_type。这些应该在body params中发送,然后才会起作用。

网址:https://login.microsoftonline.com/common/oauth2/v2.0/token client_idscoperedirect_uri 参数可以作为查询参数发送。 其中grant_typecodeclient_secret 应该在正文参数中发送。

grant_type:authorization_code, 
code: {code you got from the authorization step}, 
client_secret: ****

【讨论】:

  • 将参数转换为表单数据 - stackoverflow.com/a/47630754/1481519
  • client_id、scope和redirect_uri也必须在body中发送
  • 天哪,这服务太垃圾了!
  • 如果您使用 axios 正确创建参数,请阅读 this。所有参数都以这种方式进行,它对我有用。
【解决方案2】:

当提供“Default Scope”值必须是全名示例时,“User.Read”正确值可以从 azure AD APP -> Api Permission 获得

【讨论】:

    【解决方案3】:

    您需要将正文中的所有内容都传递为form-data

    curl --location --request POST 'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token' \
    --form 'grant_type=authorization_code' \
    --form '<the code you have got from the authorization endpoint' \
    --form 'client_secret=****' \
    --form 'client_id=********' \
    --form 'scope=m_sScope' \
    --form 'redirect_uri=http://localhost/'
    

    【讨论】:

    • 这是否也适用于 auth 部分?我在授权端点处收到此错误,据我所知,它出现在令牌端点调用之前。
    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 2020-12-29
    • 2021-10-18
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2021-10-20
    相关资源
    最近更新 更多