PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → oauth2 error AADSTS90014: The request body must contain the following parameter: 'grant_type'
oauth2 error AADSTS90014: The request body must contain the following parameter: 'grant_type'
Iniciado por guest, 27,mar. 2018 14:44 - 4 respuestas
Publicado el 27,marzo 2018 - 14:44
From the development in Windev I use Oauth 2.0 for authorization to get access to the outlook mail from a user.

The application is registered at https://apps.dev.microsoft.com without the Implicit workflow. After the user enters the credentials, an Authorization Code is returned. With the new code the Bearer Token is requested with a HTTP Post command.

So far, so good.

Only that the response gives an error message that makes no sense to me.

In code:

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
In a logfile I requested the used parameters and the content of the 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'.
The grant_type is in the header as it is supposed to be.

Does anybody have any clue of what is needed to get the OAUTH2 working ?

With kind regards,

\\Adjan
Publicado el 28,marzo 2018 - 10:40
Hi Adjan,

Quote


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

The grant_type is in the header as it is supposed to be.

Checking the error, it looks like the 'grant_type' should not be in the Header but in de POST-data body
So delete/slash all the [cMyRequest..Header]'s and unslash the

Quote


//cMyRequest..Content = m_sHTTPUrl

line, to start with.

--
Peter
Publicado el 28,marzo 2018 - 12:25
Hello Peter,

Thanks for your reply and suggestion to work with.

I tried it, with the same error message.
I also tried it with JSON (ContentType = "application/json", with VariantToJSON)
and with both in the header and in the body,
still the same error message.

I have the feeling that the error message is not intended to give a real message,
but something else is the matter,
but can't get it working.
Publicado el 28,marzo 2018 - 13:01
Hello Peter,

Thanx for the direction.

The problem was in the content of the httpRequest. This in WinDev of type Buffer.
And sometimes a buffer is loaded with zero's.
To solve the problem is to use StringToUTF8:

cMyRequest is httpRequest
cMyRequest..Method = httpPost
cMyRequest..URL = m_sTokenURL
cMyRequest..ContentType = "application/x-www-form-urlencoded"
cMyRequest..Content = StringToUTF8(m_sHTTPUrl)
cMyResponse is httpResponse = HTTPSend(cMyRequest)
m_sHTTPres = cMyResponse.Content

This is working like a charme!
Publicado el 28,marzo 2018 - 15:35
Hi Adjan,

Thanks for the complete solution (StringToUTF8), can eventually help me next time.