title: How to detect the redirect to retrieve access_token via java
body: <p>I'm starting to use facebook Graph API and I'm going to retrieve an access token with some simple HTTP requests via java.</p>
  
  <p>Following <a href="https://developers.facebook.com/docs/authentication/" rel="nofollow">https://developers.facebook.com/docs/authentication/</a>
  I created a new app but I don't have a domain so 
  I make an HTTP request to </p>
  
  <blockquote>
    <p>www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&amp;<br>
    redirect_uri=https://www.facebook.com/connect/login_success.html</p>
  </blockquote>
  
  <p>for a server-side flow, and I suppose to get redirect to a success page with a code in the URL. Then I would use this code make another HTTP request to </p>
  
  <blockquote>
    <p>graph.facebook.com/oauth/access_token?
         client_id=YOUR_APP_ID&amp;redirect_uri=YOUR_URL&amp;
         client_secret=YOUR_APP_SECRET&amp;code=THE_CODE_FROM_ABOVE</p>
  </blockquote>
  
  <p>and finally get my access token.</p>
  
  <p>I used both <code>java.net.HttpURLConnection</code> and <code>org.apache.http.HttpResponse</code>,
  but, in both cases, executing the first call I get as response the HTML of a Facebook login page.</p>
  
  <p>If I use this HTML to create a webpage and then I simply click on the Login button (without inserting username and password) I get the success page with the code!</p>
  
  <p>In the HTML the field submit of the button Login is empty and I can't retrieve redirect URLs... I can just read an alternate link in the <code>&lt;meta&gt;</code> tag which generate an auth_token (what is it? It is very different wrt an normal access_token...).</p>
  
  <p>So what I ask is:</p>
  
  <ol>
  <li><p>it is possible to detect the hidden redirect in some way just
  using <code>java.net.HttpURLConnection</code> or
  <code>org.apache.http.HttpRespons</code>e?</p></li>
  <li><p>if yes, how is the mechanism? Is it related to the <code>auth_token</code>?</p></li>
  <li><p>if no, is it possible with other libraries? (I used also restfb,
  but they seems to require an access token inserted "by hand" as an
  arg, and I also saw facebook-java-api but it seems old).</p></li>
  </ol>
  
  <hr>
  
  <p>Also if I'm logged in Facebook, executing the first HTTP call via Java I get as response the HTML of a Facebook login page. </p>
  
  <p>Using HTML to create a webpage and then I simply click on the Login button (without inserting username and password) I get the success.htm page with the code parameter in the URL.</p>
  
  <p>If I use the original URL directly in my browser I can directly obtain the success.htm page without passages in the middle.</p>
  
  <p>So I suppose the problem is in the management of cookies: in Java (executed in Eclipse) I cannot access my browser's cookies.</p>
  
  <p>I tried to redirect to use a Servlet but I get the error about the domain:
  ServletURL is not a Facebook domain or a "site URL" registered for my app (actually I did't set a site URL for my app... and that's the problem core).</p>
  
  <p>In any case here
  <a href="http://developers.facebook.com/docs/authentication/" rel="nofollow">http://developers.facebook.com/docs/authentication/</a>
  in the section App types > Desktop apps they say:</p>
  
  <blockquote>
    <p>[...] After the user authorizes your app [I allowed everything], we
    redirect the user back to the redirect_uri with the access token in
    the URI fragment: [...]</p>
    
    <p>Detect this redirect and then read the access token out of the URI
    using whatever mechanisms provided by your framework of choice. [...]</p>
  </blockquote>
  
  <p>So I think that it is still possible to detect this redirect via Java. How?</p>