title: Facebook Access Token - Automate Getting the Token
body: <p><strong>Go below to see answer</strong></p>
  
  <p>I am getting the Facebook access token fine, but where I am having troubles is when I am trying to automate this process. </p>
  
  <p>If I visit this URL is the browser I get the access token just fine.</p>
  
  <p>Example:</p>
  
  <p>I paste this into the browser and hit return.</p>
  
  <p><a href="https://www.facebook.com/dialog/oauth?client_id=324234343434&amp;scope=[&#39;ads_read&#39;,%20&#39;ads_management&#39;]&amp;redirect_uri=http://www.kb-demos.com/login_success.html&amp;response_type=token" rel="nofollow noreferrer">https://www.facebook.com/dialog/oauth?client_id=324234343434&amp;scope=['ads_read', 'ads_management']&amp;redirect_uri=http://www.kb-demos.com/login_success.html&amp;response_type=token</a></p>
  
  <p>Then I get sent to this page: </p>
  
  <p><a href="http://www.kb-demos.com/login_success.html?#access_token=34543534534534KJ534LKJLKJLKHLH4534534J5KH345KJ3L4H53KJ5H3K4LJH34KH54K&amp;expires_in=5180653" rel="nofollow noreferrer">http://www.kb-demos.com/login_success.html?#access_token=34543534534534KJ534LKJLKJLKHLH4534534J5KH345KJ3L4H53KJ5H3K4LJH34KH54K&amp;expires_in=5180653</a></p>
  
  <p><em>I changed the access token piece so its not a real token</em></p>
  
  <p>Viola access token!</p>
  
  <p>What I am trying to do is replicate that same behavior with code. I am getting close but not quite there. </p>
  
  <p>I keep getting the user_denied error.</p>
  
  <p>%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%23_%3D_&amp;display=page&amp;locale=en_US&amp;logger_id=786a3153-1d81-415c-8dca-f8fa8b0cd630</p>
  
  <p>I am outputting all headers to the console. It is the location header I am concerned with**</p>
  
  <p><em>I think it has to do with the 302 redirect?</em></p>
  
  <pre><code>ApplicationId = request.ClientId;
  string permissions = "['ads_management', 'ads_read']";
  
  var destinationURL = String.Format(
  @"https://www.facebook.com/dialog/oauth?client_id={0}&amp;scope={1}&amp;redirect_uri=http://www.kb-demos.com/login_success.html&amp;response_type=token",
  ApplicationId,
  permissions);
  
  // Create a new 'HttpWebRequest' Object to the mentioned URL.
  HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(destinationURL);
  myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
  
  myHttpWebRequest.AllowAutoRedirect = false;
  // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
  HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  
  //Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.Headers); // my http headers
  // Print the HTML contents of the page to the console. 
  
  
  var headers = myHttpWebResponse.Headers;
  
  // output all the headers
  foreach(var header in headers) {
      Console.WriteLine(header.ToString() + ": " + headers[header.ToString()] + "\n" );
  
  }
  
  #region
  //Stream streamResponse = myHttpWebResponse.GetResponseStream();
  //StreamReader streamRead = new StreamReader(streamResponse);
  //Char[] readBuff = new Char[256];
  //int count = streamRead.Read(readBuff, 0, 256);
  //Console.WriteLine("\nThe HTML contents of page the are  : \n\n ");
  //while (count &gt; 0)
  //{
  //    String outputData = new String(readBuff, 0, count);
  //    Console.Write(outputData);
  //    count = streamRead.Read(readBuff, 0, 256);
  //}
  //// Close the Stream object.
  //streamResponse.Close();
  //streamRead.Close();
  // Release the HttpWebResponse Resource.
  #endregion
  
  
  myHttpWebResponse.Close();
  
  Console.ReadLine();
  </code></pre>
  
  <p>I am getting a user_denied error here. But in the browser I am getting a perfectly good token. I cannot figure out why. </p>
  
  <p><a href="https://i.stack.imgur.com/dpeXQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dpeXQ.png" alt="enter image description here"></a></p>
  
  <p>The headers in the location header it seems to work when using the browser.</p>
  
  <p>Possible scenario if I cannot get the above to work:
  I was wondering if there is a browser with an API? Something I can call from the command line - pass in some arguments - and then get the redirect url is a variable to parse?</p>