title: Facebook: How to retrieve an access token programmatically?
body: <p>I need to post a message on my <em>own</em> Facebook page; and I need to do it <em>programmatically</em> (in my case using Python). I managed to do this part using this code (in Python):</p>
  
  <pre><code>import urllib, urllib2
  
  access_token='XXXX'
  fb_page_id='YYYY' # my page ID
  
  post_data = {'access_token':access_token, 'message':'hey this is a test!'}
  request_path = str(fb_page_id)+'/feed'
  post_data = urllib.urlencode(post_data)
  response = urllib2.urlopen(
      'https://graph.facebook.com/%s' % request_path, post_data
  )
  </code></pre>
  
  <p>The ID of the generated post on the FB page is correctly returned:</p>
  
  <pre><code>In [11]: response.readlines()
  Out[11]: ['{"id":"135386143198208_461964357207050"}']
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>In order to generate the <code>access_token</code> and make the API request above I had to <em>manually</em> follow the three steps detailed <a href="https://developers.facebook.com/docs/howtos/login/login-as-page/">here</a>.</p>
  
  <p>But in practice this manual process is unacceptable <strong>as I need to run this task from a cron job</strong>. Hence I need to automate it because <code>access_token</code> in Facebook is temporary. I.e. I need to get an access token each time I run this script. How to do that?</p>
  
  <p>Feel free to use any scripting tool in your answer (curl, JavaScript, Java, PHP) as long you communicate the steps involved. Note that I need to do this using any server-side language (Python/Ruby/PHP).</p>