Using cURL in Ruby on Rails to Force Facebook to Rescrape a URL

I recently ran into a problem with the Facebook share button on my blogging platform, Bloopist. Whenever I shared an post, the share box from Facebook showed an incorrect image and description of the post. This led me to ask the question, "How can I force Facebook to rescrape or update a URL or Open Graph object?"

It took a little bit of Googling, but I was able to find the solution pretty quickly. The manual way to do it, which works if you have to update just one or two links, is to use the Open Graph Object Debugger. (Alternatively, you can just wait 30 days since Facebook says they rescrape every page every 30 days. That's not a great solution though because that may not be true in the future.)

A better way to do it is to POST an update request to Facebook directly. This can be done on the command line using curl, and since curl is a system command, it can be easily called from any program.

If you'd like to learn more about curl, check out The Linux Command Line: A Complete Introduction.

My blog platform runs on Ruby on Rails, so I wrote the quick snippet of Ruby code below to tell Facebook to rescrape the URL for one of my blog's articles. I checked the last scrape time of the article on the Facebook debugger before and after running this code to confirm that it worked. (And it did work. Yay!)

url = CGI.escape("https://www.google.com/")
system("curl -d 'id=#{url}&scrape=true' -X POST https://graph.facebook.com/")

I should mention that the main difficulty I faced in finding this solution was that Facebook changed the endpoint for this command at least once. The internet changes fast, so you should always make sure you're referring to the most up to date source!

If this helped you, let me know in the comments.

Sources

Photo by Jason Jacobs