# Facebook Developer
# Create an App
The first step is to set up a facebook developer account. It is free, go to this url: https://developers.facebook.com/ (opens new window) to get started.
Click on "Create new App", select business app and continue with the process to get your app set-up.
Go to the next step, enter the app name and press the green button at the bottom to create the app.
Click at the tools at the top of the page, and go to the graph api explorer.
Link to Graph API Explorer (opens new window)
# Get Access Token from GraphAPI
You will need to get your access tokens in order to post to instagram with the API. There are different types of access tokens that we will look at.
When you navigate to the graph explorer, make sure you select the appropriate permissions and project as shown below.
- Make sure the correct app or project is selected. You can have multiple apps, select the correct one from the drop-down list.
- Select the permissions required from the drop down, use the list in (3) to guide you. These are the permissions you need to work with instagram automated posting
- Make sure all these permissions are selected.
TIP
At this stage we need the user access token, we will exchange it later for a page token. You can only get short lived token from the explorer. We will explain this later.
# Exchange Tokens
The token that you get from the graph explorer is short-lived. It only lasts for an hour, therefore you cannot write python code with it. What is required is a long-life token that lasts a couple of months.
Good news is that, you can exchange your short-life token for a long-life token to use in the python code. The different types of token we will be dealing with in this tutorial is:
- Short life user access token
- Long life user access token
- Long life page access token
We will use each token to get the next one on the list, in numerical order.
# Exchange Short Life User Token
The first step is to exchange the short life user token for the long life user token. Grab the token from the steps above and use it in the following request:
What you will need is:
- Client ID: clientId
- Client Secret: clientSecret
- Short Life User Access Token: accessToken
Perform the following GET request
https://graph.facebook.com/v10.0/oauth/access_token?grant_type=fb_exchange_token&client_id=<*clientId*>&client_secret=<*clientSecret*>&fb_exchange_token=<*accessToken*>
The response will look like this:
{
"access_token": "<long life user access token here>",
"token_type": "bearer"
}
# Exchange the Long Life User Token
The next step is to exchange the long life user token for the long life page access token. At this stage you should already have a facebook page, and this facebook page should be connected to your instagram page. The instagram page should be a professional instagram page.
Check out the full video here for Instagram Graph API Automation (opens new window).
What you will need:
- Your Facebook Page Number: pageNumber
- The page access token: pageAccessToken
Perform the following GET request
https://graph.facebook.com/v13.0/<pageNumber>/accounts?access_token=<pageAccessToken>
The response will look like this:
{
"data": [
{
"access_token": "<longLifePageToken>",
"category": "<category>",
"category_list": [
{
"id": "<someID>",
"name": "<category>"
}
],
"name": "<pageName>",
"id": "<pageNumber>",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MESSAGING",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
}
],
"paging": {
"cursors": {
"before": "<someStuff>",
"after": "<someStuff>"
}
}
}
For this step you will notice, you need the page number. There are some steps to follow to get the pageID, first you need the userID.
# User and Page ID
# Get User ID
You will need:
- User Access Token (either long life or short life): userToken
Run the following GET command:
https://graph.facebook.com/me?<userToken>
The response you will get:
{
"name": "<Facebook User Name>",
"id": "<Facebook User ID>"
}
# Get Page ID
You will need:
- User Access Token (either long life or short life): userToken
- User ID - from step above: userID
https://graph.facebook.com/<userID>/accounts?access_token=<userToken>
What you will get in return:
{
"data": [
{
"access_token": "<userToken>",
"category": "Software",
"category_list": [
{
"id": "2211",
"name": "Software"
}
],
"name": "<pageName>",
"id": "<pageID>",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MESSAGING",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
}
],
"paging": {
"cursors": {
"before": "<someStuff>",
"after": "<someStuff>"
}
}
}