OneDrive for Business: "invalid_request", "error_description": "AADSTS90014: the request body must contain the following parameter:" grant_type

I am trying to integrate OneDrive for Busines into a Web Form application. For this, I use the documentation provided at this URL https://dev.onedrive.com/auth/aad_oauth.htm In the Web Form application, I have two pages. The first is the login page, which has a button to enter system When I click on the login button, I make a GET request for OneDrive for business API using the following code

HttpClient client = new HttpClient(); Redirecturi = Uri.EscapeDataString(Redirecturi); string url = string.Format("https://login.windows.net/common/oauth2/authorize?response_type=code&client_id={0}&redirect_uri={1}", ClienId, Redirecturi); var response = client.GetAsync(url); var json = response.Result.Content.ReadAsStringAsync(); Label2.Text = json.Result; 

When I press the login button, I take me to micorosoft support and send me back to the callback.aspx page with the access code (URi redirection is set to light blue)

I got the access code. On the second page, I can fix the access code and make a POST request to get the authentication token. Here is the code for the second page.

 private string BaseUri="https://login.windows.net/common/oauth2/token"; public string Redirecturi = "http://localhost:51642/CallBack.aspx"; public string ResourcesId = "https://api.office.com/discovery/"; private string ClienId = "180c6ac4-5829-468e-.....-822405804862"; ///truncated//azure private string ClientSecert = "G4TAQzD8d7C4...OE6m366afv8XKbTCcyXr4=";//truncated protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request.QueryString[OAuthConstants.AccessToken])) { // There is a token available already. It should be the token flow. Ignore it. return; } if (!string.IsNullOrEmpty(Request.QueryString[OAuthConstants.Code])) { string _accessCode = Request.QueryString[OAuthConstants.Code]; HttpClient client = new HttpClient(); // BaseUri = Uri.EscapeDataString(BaseUri); Redirecturi = Uri.EscapeDataString(Redirecturi); ResourcesId = Uri.EscapeDataString(ResourcesId); string url = string.Format("{0}?client_id={1}&redirect_uri={2}&grant_type=authorization_code&client_secret={3}&code={4}&grant_type=authorization_code&resource={5}", BaseUri, ClienId, Redirecturi, ClientSecert, _accessCode, ResourcesId); var response = client.PostAsync(url, null); var json = response.Result.Content.ReadAsStringAsync(); Response.Write(json); } } 

But instead of the answer, I get the following error. By the way, specify type grant_type in url. I already added (you can check the code). Without including this, I also get the same error.

Here is the error

 {"error":"invalid_request","error_description":"AADSTS90014: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: 2adb3a7f-ceb1-4978-97c4-3dc2d3cc3ad4\r\nCorrelation ID: 29fb11a0-c602-4891-9299-b0b538d75b5f\r\nTimestamp: 2015-07-15 09:58:42Z","error_codes":[90014],"timestamp":"2015-07-15 09:58:42Z","trace_id":"2adb3a7f-ceb1-4978-97c4-3dc2d3cc3ad4","correlation_id":"29fb11a0-c602-4891-9299-b0b538d75b5f","submit_url":null,"context":null} 

please help find out where, what happened wrong. Any help would be appreciated Thank you in advance

+5
source share
2 answers

You add parameters to the query request. You must send data to the request body.

 var content = new StringContent( "grant_type=authorization_code" + "&client_id=" + ClienId + "&redirect_uri=" + Redirecturi + "&client_secret=" + ClientSecert + "&code=" + _accessCode + "&resource=" + ResourcesId, Encoding.UTF8, "application/x-www-form-urlencoded"); var response = httpClient.PostAsync(BaseUri, content); var result = response.Result.Content.ReadAsStringAsync(); 
+5
source

use FormUrlEncodedContent instead of StringContent (form data message)

 var formContent = new FormUrlEncodedContent(new Dictionary<string, string> { { "client_id", clientId }, { "client_secret", clientSecret }, { "code", authCode }, { "redirect_uri", redirectUri }, { "grant_type", "authorization_code" } }); var response = await httpClient.PostAsync("https://login.microsoftonline.com/common/oauth2/token", formContent); 
0
source

All Articles