Lo-Fi Python

Oct 04, 2020

Post an Unsplash Photo to Facebook Pages with Python

My goal was to automate posting positive photos and quotes to my Facebook page, "Positive Thoughts Daily", with the Unsplash and Facebook APIs. Here's how I did it!

This implementation relies on my own collection of photos on Unsplash. I will manually select which photos I like to get added to my collection. Then my app will take the new photos and post one a day for me.

Side note: the free version of the Unsplash API is capped at 50 requests per week, which was enough for me.

Setting Up The Facebook API

  1. Sign up for a Facebook developer account
  2. Create a new Facebook app
  3. Add "page_manage_posts" and "pages_read_user_content" permissions to your app in the Graph API Explorer
  4. Get a "short lived" user access token from the Graph API explorer (optional: fetch a "long lived" user access token, which lasts up to 60 days)
  5. Use your user access token to a fetch your page's access token

Optional: fetch long lived access token with curl

curl -i -X GET "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token& client_id={app-id}& client_secret={app-secret}& fb_exchange_token={short-lived-user-access-token}"

Fetch your Facebook page's access token

curl -i -X GET "https://graph.facebook.com/{your-user-id}/accounts?access_token={user-access-token}

Setting up Unsplash

  1. Sign up for an Unsplash developer account
  2. Install the python-unsplash library. In the terminal enter:
python -m pip install python-unsplash
  1. Decide what photo you want to post. This example fetches a random photo from my Unsplash collection. You can also fetch any photo at random, or pass in a query to get a certain type of photo.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from unsplash.api import Api
from unsplash.auth import Auth
import requests
"""Python-Unsplash library Github:
https://github.com/yakupadakli/python-unsplash
"""
client_id = "add_your_client_id"
client_secret = "add_your_client_secret"
redirect_uri = "add_your_redirect_uri"
code = ""
auth = Auth(client_id, client_secret, redirect_uri, code=code)
api = Api(auth)
# returns a python list containing a class
image = api.photo.random(collections=66610223) # my collection id
image_id = image[0].id
# Use image_id to get random photo's download link from a collection.
url = f"https://api.unsplash.com/photos/{image_id}/download?client_id={client_id}"
r = requests.get(url)
print(r.text)
image = r.json()
download_link = image['url']

Posting the Unsplash Image to Facebook

1
2
3
4
5
6
"""Use download link and post to page with Facebook API."""
page_id = "add_page_id_from_about_section"
url = f"https://graph.facebook.com/{page_id}/photos?access_token={page_access_token}&url={download_link}"
r = requests.post(url)
post_ids = r.json()
print(post_ids)

Post Project Reflections

This was my first time working with the Facebook API. Honestly, it's a little crazy trying to balance all the token types in your head. There are about 5 different types of tokens that are used for different things! Ultimately I was able to figure out how to to post a photo. So there is a bit of a learning curve. It's a good challenge to build your API skills. The Unsplash API requires no Oauth tokens and is easier to pick up.

My Facebook page posts are now triggered by page loads on this website! I am using a MySQL database to track which images I post to make sure I don't duplicate any posts and to make sure I only post once every 24 hours. Ah, I love the smell of fresh automation in the morning. 😀

Supplementary Links