title: Building a Windows Forms application and trying to receive an access token from Paypal
body: <p>I am building a Windows Forms application with Visual Studio 2010 and .NET Framework 4.</p>
  
  <p>I am trying to receive an access token from paypal using c# HTTP POST request. I keep getting the following message:</p>
  
  <blockquote>
    <p>System.Net.WebException: The remote server returned an error: (401) Unauthorized.<br>
    at System.Net.HttpWebRequest.GetResponse()</p>
  </blockquote>
  
  <p>Am I missing something in my code?</p>
  
  <p>Thanks alot!</p>
  
  <p>This is my code:</p>
  
  <pre><code>        string ClientID = "1234";
          string secret = "****";
  
          WebRequest Request = WebRequest.Create(" https://api.sandbox.paypal.com/v1/oauth2/token");
          Request.Method = "POST";
          Request.ContentType = "application/x-www-form-urlencoded";           
          string PostData = System.Uri.EscapeDataString(String.Format("Accept=application/json&amp;Accept-Language=en_US&amp;user-id={0}&amp;password={1}&amp;grant_type=client_credentials", ClientID, secret));
          byte[] ByteArray =Encoding.Default.GetBytes(PostData);
          Request.ContentLength = ByteArray.Length;
          Request.Headers["Authorization"] = "Basic " + ByteArray;
          Stream dataStream;
  
          using ( dataStream = Request.GetRequestStream())
          {
              dataStream.Write(ByteArray, 0, ByteArray.Length);
              dataStream.Close();
          }
  
          WebResponse respones = Request.GetResponse();
          Console.WriteLine(((HttpWebResponse)respones).StatusDescription);
          Console.WriteLine(((HttpWebResponse)respones).Server);
          Console.WriteLine(((HttpWebResponse)respones).ResponseUri);
  
          dataStream = respones.GetResponseStream();
          StreamReader sReader = new StreamReader(dataStream);
          string ResponesFromServer = sReader.ReadToEnd();
  
          Console.WriteLine(ResponesFromServer);
  
          dataStream.Close();
          sReader.Close();
          respones.Close();
  </code></pre>