title: Azure AD OAuth2 Access Token Request Error - 400 Bad Request
body: <p>My WPF desktop application (C#) is attempting to read the user's Outlook emails through the Microsoft Graph API. I am stuck in the authentication process; I've already received an authentication code and now I'm trying to get an access token from Azure but keep getting a HTTP 400 error code when sending out the request for the <em>access token</em>:</p>
  
  <pre><code>/**** Auth Code Retrieval ****/
  string authCodeUrl = "https://login.microsoftonline.com/common/oauth2/authorize";
  authCodeUrl += "?client_id" = clientId;
  authCodeUrl += "&amp;redirect_uri=" + redirectUri;
  authCodeUrl += "&amp;response_type=code";
  authCodeUrl += "&amp;resource=https%3A%2F%2Fgraph.microsoft.com%2F";
  Process.start(authUrl); // User logs in, we get the auth code after login
  string code = "......"; // Hidden for this post
  
  /**** Access Token Retrieval ****/
  string tokenUrl = "https://login.microsoftonline.com/common/oauth2/token"
  string content = "grant_type=authorization_code";
  content += "&amp;client_id=" + clientId;
  content += "&amp;resource=https%3A%2F%2Fgraph.microsoft.com%2F";
  content += "&amp;code=" + code;
  content += "&amp;redirect_uri=" + redirectUri;
  WebRequest request = WebRequest.Create(tokenUrl);
  request.ContentType = "application/x-www-form-urlencoded";
  byte[] data = Encoding.UTF8.GetBytes(content);
  request.ContentLength = data.Length;
  request.Method = "POST";
  try 
  {
    using (Stream stream = request.GetRequestStream())
    {
      stream.Write(data, 0, data.Length);
    }
    WebResponse response = request.GetResponse(); // This throws exception
  }
  catch (Exception error) // This catches the exception
  {
    Console.WriteLine(error.Message); // Outputs 400, bad request
  }
  </code></pre>
  
  <p>The above is the code used to retrieve the auth code followed by the attempt to retrieve the access token. We do not have a client_secret because secrets are only for Web applications and this is a native desktop WPF application. I have read that this isn't an issue though. I have followed many tutorials and official docs online, mainly <a href="https://graph.microsoft.io/en-us/docs/authorization/app_authorization" rel="noreferrer">the official Graph authorization doc</a> and I still cannot figure out what I am doing wrong. Any help would be greatly appreciated, thank you.</p>