Python Facebook Oembed

April 16, 2020
import requests
from urllib.parse import urlencode

url = 'https://www.facebook.com/nixonsiow/posts/3093544747376885'
# url = 'https://www.facebook.com/brian.john.crowley/videos/10158058811332822/'

query_string = urlencode({'url': url}) # , 'omitscript': True
oembed_url = f"https://www.facebook.com/plugins/post/oembed.json?{query_string}"

# another endpoint for video, but plugins/post/oembed.json work for video link as well
# oembed_url = f"https://www.facebook.com/plugins/video/oembed.json?{query_string}"

r = requests.get(oembed_url)
if r.status_code == 200:
    result = r.json()
    html = result['html'].strip()
{
  "author_name": "Nixon Siow",
  "author_url": "https://www.facebook.com/nixonsiow/",
  "provider_url": "https://www.facebook.com",
  "provider_name": "Facebook",
  "success": true,
  "height": null,
  "html": "<div id=\"fb-root\"></div>\n<script async=\"1\" defer=\"1\" crossorigin=\"anonymous\" src=\"https://connect.facebook.net/ms_MY/sdk.js#xfbml=1&amp;version=v6.0\"></script><div class=\"fb-post\" data-href=\"https://www.facebook.com/nixonsiow/posts/3093544747376885\" data-width=\"552\"><blockquote cite=\"https://www.facebook.com/nixonsiow/posts/3093544747376885\" class=\"fb-xfbml-parse-ignore\"><p>A very very quick sketch for sharing during this difficult moment.\n#tqfrontliners #covid19\n\np/s: sorry for the bad drawing, and bad English</p>Posted by <a href=\"https://www.facebook.com/nixonsiow/\">Nixon Siow</a> on&nbsp;<a href=\"https://www.facebook.com/nixonsiow/posts/3093544747376885\">Sabtu, 21 Mac 2020</a></blockquote></div>",
  "type": "rich",
  "version": "1.0",
  "url": "https://www.facebook.com/nixonsiow/posts/3093544747376885",
  "width": 552
}

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.