Embed Facebook Post in Body using Shortcodes¶
This tutorial explains how to create an article with an embedded Facebook post in the body content.
Requirements¶
For this tutorial you will need both the Python and requests libraries installed.
Procedure¶
1. Initialize variables¶
Set the global variables.
import requests
API_DOMAIN = '<your-secure-domain>'
API_KEY = '<your-api-key>'
2. Obtain a shortcode using a media asset URL¶
This code snippet uses the Scraper API to obtain a shortcode that can be embedded in body content.
facebook_post_url = 'https://www.facebook.com/RebelMouse/posts/2343829539007928'
response = requests.get(
'https://{}/api/1.3/scraper'.format(API_DOMAIN),
params={
'api_key': API_KEY,
'url': facebook_post_url,
}
)
response.raise_for_status()
scraper_response = response.json()
print(scraper_response['embed']['shortcode'])
3. Create a draft¶
Once you have the shortcode, create a new draft using the Drafts API. This new draft will need to have the shortcode as part of its body content.
body_content = '''
<p>This is a HTML piece that can contain shortcodes</p>
{}
<p>This text goes after the embedded FB post</p>
'''.format(scraper_response['embed']['shortcode'])
response = requests.post(
'https://{}/api/1.3/drafts'.format(API_DOMAIN),
params={'api_key': API_KEY},
json={
'headline': 'Embedded FB post in article content',
'body': body_content,
}
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['id'])
4. Publish the draft¶
Finally, publish the draft and print its URL.
response = requests.put(
'https://{}/api/1.3/drafts/{}'.format(API_DOMAIN, draft_api_response['id']),
params={'api_key': API_KEY},
json={'action': 'publish'}
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['post_url'])