In this post, we will walk through programatically force updating the Facebook Open Graph data for a given link using the Open Graph API.


Prerequisites:
1. Server to run the script (You can use an Amazon EC2 Free Tier server)
2. Facebook Account


The first thing we need to do is create an extended life token for Open Graph API calls.

  1. Visit https://developers.facebook.com/apps/
    • Create a new app named Open Graph Refresher
  2. Visit https://developers.facebook.com/tools/accesstoken/
    • Under the Open Graph Refresher App section, click the need to grant permissions link on the User Token row if a token does not exist yet
    • On the right side of the User Token, click the Debug button
    • On the bottom left side of the Access Token Info table, click the Extend Access Token button and enter your password when prompted
    • Copy the new long live access token
  3. Visit https://developers.facebook.com/tools/explorer
    • Paste the long live access token from step 2 above into the Access Token field.
    • Click the Add a Field link on the bottom left side of the POST input and enter Name ‘id’, Value ‘https://pericror.com’
    • Click the Add a Field link on the bottom left side of the POST input and enter Name ‘scrape’, Value ‘True’
    • Click the Submit button. If your access token was set up correctly you should receive a response containing the open graph tags scraped from the page https://pericror.com.

Now that we can successfully use the Facebook  Open Graph API to refresh a given links open graph tags, we can use our access token and the following python script to continuously update the Open Graph cache:

import requests
import time

api_url = "https://graph.facebook.com"
access_token = "paste your long live access token here"
graph_url = "paste the link of the page you want to refresh here"
post_data = { 'id':graph_url, 'scrape':True, 'access_token':access_token }
# Beware of rate limiting if trying to increase frequency.
refresh_rate = 30 # refresh rate in seconds

while True:
    try:
        resp = requests.post(api_url, data = post_data)
        if resp.status_code == 200:
            contents = resp.json()
            print(contents['title'])
        else:
            error = "Warning: Status Code {}\n{}\n".format(
            resp.status_code, resp.content)
            print(error)
            raise RuntimeWarning(error)
    except Exception as e:
        f = open ("open_graph_refresher.log", "a")
        f.write("{} : {}".format(type(e), e))
        f.close()
        print(e)
    time.sleep(refresh_rate)

In order to keep the script running on our server, we run it in a session manager such as screen by doing screen -S refresher and then running python open_graph_refresh.py. We can then detatch the screen with the keystrokes ctrl+a+d. To resume the screen we use screen -r refresher, and to kill it once resumed we can ctrl+c to end the script and type exit.

We have now successfully set up a way to programatically force update the Facebook Open Graph for our specified link (https://pericror.com) at our specified duration (30s).

The code for this blog post can be found on our Github.