title: Authentication on Instagram to get the access_token using the API
body: <p>I'm using the Instagram API and want to get the access_token in order to throw api requests over my own account. When I try to follow the first step and get the authorization code programmatically using RestTemplate I can't get it work. </p>
  
  <pre><code>String AUTHORIZE_URL = "https://api.instagram.com/oauth/authorize/?client_id=&lt;CLIENT_ID&gt;&amp;redirect_uri=&lt;REDIRECT_URI&gt;&amp;response_type=code";
  String url = String.format(AUTHORIZE_URL, clientId, redirectUri);
  String o = restTemplate.getForObject(url, String.class);
  </code></pre>
  
  <p>The response is the html code of the login page because Instagram requires the user to be logged in to check if the app is authorized (of course it is, since the app an the user belongs to my own account).</p>
  
  <p><strong>How can I authenticate before throwing that request so they return the code to my redirectUri and not complain about login?</strong>
  Note: I tried simulating the request to their login form but it returned a 403 Forbidden.</p>
  
  <p>NOTE: I already got a valid access_token, manually generated, and it works perfectly but I want to implement also a process to re-generate a new access_token automatically since they may invalidate it at any time in the future.</p>
  
  <blockquote>
    <p>Even though our access tokens do not specify an expiration time, your app should handle the case that either the user revokes access, or Instagram expires the token after some period of time. If the token is no longer valid, API responses will contain an “error_type=OAuthAccessTokenError”. In this case you will need to re-authenticate the user to obtain a new valid token.
    In other words: <strong>do not assume your access_token is valid forever</strong>.</p>
  </blockquote>