Using Facebook API to force Facebook debugger to scrape your web page

Whenever you share a website or any link on Facebook, Facebook tries to parse your page and display title, description,image based on the info provided by the metatags of your website. Facebook looks for OG metatags and displays the data on the basis of them. But once Facebook parses/scrapes your pages, it caches it into its memory. So if you make any changes to the metatags, they don’t get reflected. And if you share your website, Facebook will still show the old info. We can use Facebook debugger tool to manually force Facebook to scrape the page again. But what if you want this process to be automated. Well, we do have a Facebook API which does this.

This function expects the URL to be scraped and number of times you want to try it. Sometimes even after calling this API, Facebook still fetches info from cache and does not scrape the page again. So you have to call it multiple times. If the response contains the ‘key’, then we can assume page has been scraped. You can use ‘image’ as the key

You should also be having Facebook’s access token to invoke this API.

public static void lint(final String urlendpoint, final int count,
			final HttpServletRequest req, final int maxattempts, final String key) {
		new Thread() {
			String session = (String) req.getSession().getAttribute("session");
 
			public void run() {
				_lint(urlendpoint, count,key);
			}
 
			private void _lint(String urlendpoint, int count, String key) {
 
				String query = "id=" + urlendpoint
						+ "&scrape=true&access_token=" + session;
				String ids = "";
				BufferedReader br = null;
				try {
					URL url = new URL("https://graph.facebook.com");
 
					HttpURLConnection urlc = (HttpURLConnection) url
							.openConnection();
					urlc.setRequestMethod("POST");
 
					urlc.setDoOutput(true);
					urlc.setAllowUserInteraction(false);
					PrintStream ps = new PrintStream(urlc.getOutputStream());
					System.out.println("query - " + query + " - attempt "
							+ count);
					ps.print(query);
					ps.close();
					br = new BufferedReader(new InputStreamReader(
							urlc.getInputStream()));
					String l = null;
					while ((l = br.readLine()) != null) {
						ids += l;
					}
					JsonObject jobj = new JsonObject(ids);
					System.out.println(jobj);
					if (!jobj.has(key) && count < maxattempts) {
						_lint(urlendpoint, ++count, key);
					}
 
				} catch (Exception e) {
					System.out.println("For app " + query + " and count "
							+ count + " Exception is " + e);
					/*if (count < 7)
						_lint(urlendpoint, ++count, key);*/
				} finally {
					try {
						if (null != br) {
							br.close();
						}
 
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
 
			}
		}.start();
	}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *