I am trying to use the AD v2 Oath authorization code flow for one drive. App is registered correctly on my account as Web platform. I am able to retrieve an authorization code but when I am going to use it in order to obtain a token I get always
{
"error": "invalid_grant",
"error_description": "AADSTS70000: The provided value for the 'code' parameter is not valid. The code has expired.\r\nTrace ID: 95d076f5-6cfa-4de1-ba1a-a81066cf1200\r\nCorrelation ID: 4cddfa50-228b-431a-8746-483e77a273e1\r\nTimestamp: 2017-06-10 09:50:38Z",
"error_codes": [
70000
],
"timestamp": "2017-06-10 09:50:38Z",
"trace_id": "95d076f5-6cfa-4de1-ba1a-a81066cf1200",
"correlation_id": "4cddfa50-228b-431a-8746-483e77a273e1"
}
my request
Dim url As New System.Text.StringBuilder
url.Append("grant_type=authorization_code")
url.Append("&client_id=" + clientid)
url.Append("&client_secret=" + clientsecret)
url.Append("&redirect_uri=" + System.Web.HttpUtility.UrlEncode(redirecturl))
url.Append("&code=" + authcode)
url.Append("&scope=" + ("offline_access%20files.readwrite"))
Dim baseaddress = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(url.ToString)
Dim req = Net.HttpWebRequest.Create(baseaddress)
req.Proxy = Nothing
req.Method = "Post"
req.ContentType = "application/x-www-form-urlencoded"
Dim strm As System.IO.Stream = req.GetRequestStream()
strm.Write(buffer, 0, buffer.Length)
strm.Close()
Dim resp As System.Net.HttpWebResponse = req.GetResponse()
Dim response = New IO.StreamReader(resp.GetResponseStream, False).ReadToEnd
Return response
The mistake was with the redirect url. I had set the production url and after that i was copying the code to localhost in order to debug the token process. When i change the redirect url of the request to locahost worked like a charm. My opinion is that should be mentioned on their documentation (i had never that kind of problem on other APIs like google drive's)