23

在 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 工作需要什么?

4

3 回答 3

24

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

Url:https://login.microsoftonline.com/common/oauth2/v2.0/token client_idscope参数redirect_uri可以作为查询参数发送。在哪里grant_typecode并且client_secret应该在正文参数中发送。

grant_type:authorization_code, 
code: {code you got from the authorization step}, 
client_secret: ****
于 2019-03-05T09:52:03.240 回答
15

您需要将正文中的所有内容传递为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/'
于 2020-10-14T12:55:16.193 回答
0

当提供“默认范围”值必须是全名示例时,“User.Read”正确值可以从 azure AD APP -> Api Permission 中获取

于 2019-10-15T07:10:57.043 回答