oauth2错误AADSTS90014:请求正文必须包含以下参数:'grant_type'

Adj*_*jan 6 http windev oauth-2.0 bearer-token

从Windev的开发中,我使用Oauth 2.0进行授权以从用户访问Outlook邮件。

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

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

只是该响应给出了一条对我没有意义的错误消息。

在代码中:

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
Run Code Online (Sandbox Code Playgroud)

在一个日志文件中,我请求使用的参数和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'.
Run Code Online (Sandbox Code Playgroud)

grant_type应该在标头中。

是否有人对使OAUTH2工作需要什么有任何线索?

小智 20

你不应该grant_type在 params 和 headers 中发送。那些应该在正文参数中发送,然后只有它才能工作。

网址:https://login.microsoftonline.com/common/oauth2/v2.0/token client_idscoperedirect_uriPARAMS可以被作为查询参数。其中 as grant_typecode并且client_secret应该在正文参数中发送。

grant_type:authorization_code, 
code: {code you got from the authorization step}, 
client_secret: ****
Run Code Online (Sandbox Code Playgroud)

  • 天哪,这服务真是一坨屎! (7认同)
  • 如果您使用 axios 正确创建参数,请阅读[此](https://gist.github.com/akexorcist/ea93ee47d39cf94e77802bc39c46589b)。所有参数都以这种方式进行,它对我有用。 (3认同)
  • 将参数转换为表单数据 - /sf/answers/3334152811/ (2认同)

小智 10

您应该将内容类型更改为:application/x-www-form-urlencoded

正文的格式必须如下:

 client_id=8cfbe8ac-8775-4c56-9302-k9d5a42cbf98
 &client_secret=BOy7Q~pGvXF.SWshX72mmMnQeAkvN5elHWiYT
 &grant_type=client_credentials
 &resource=https://miurl.com
Run Code Online (Sandbox Code Playgroud)


Mar*_*ola 8

您需要将 body 中的所有内容传递为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/'
Run Code Online (Sandbox Code Playgroud)


Atu*_*tul 6

如果有人仍然遇到此问题,您可以从邮递员处尝试,如下所示。请检查正文类型为“x-www-form-urlencoded”

在此输入图像描述