Skip to content
JeevanGuptaKnowledge means the world to me
  • API Setup
  • Python3
  • Web Devop
  • Travel
  • Social
  • ANN
  • About
    • About Jeevan Gupta
    • Connect with Jeevan Gupta

Facebook API setup guide – Facebook Developer Account Setup

  • Post author By Jeevan Gupta
  • Post date August 7, 2020

Facebook API – How to get an OAuth access token and how to call the API to get data – Step-by-step guide

Hi Everyone, hope you are keeping well. Thank you so much for stopping by. Today we are going to set up a Facebook developer account to use Facebook API, create an app in the developer account, enable Facebook Marketing API, and eventually make our first Facebook API call using python. In short, we will see how to set up and use Facebook API.

The Facebook Marketing API is used to programmatically query data, create and manage ads, and perform a wide variety of other tasks. This guide helps you complete all the steps needed to use marketing API successfully.

Since the API is HTTP-based, it works with any language or software that supports HTTP, including CURL and almost all modern web browsers. Other methods are packages that we can import in code files (Python) to use this API.

Steps involved – to use Facebook API (Facebook Marketing API) successfully:- 

Table of Content:

  • Create App on Facebook Developer
  • Generate Access Token
  • Get Ad Account ID
  • Get App ID
  • Get App Secret
  • Make First API call

1. Create an App under Facebook Developer:

Let’s first go to Facebook Developer. After that navigate to the “My Apps” drop-down in the top right corner and select “Add a New App” / “Create New App”. Choose a display name and a category and then click on “Create App ID”.

2. Generating Credentials:

How to get Access Token?

Under “Products” click on “+” and add “Marketing API” by clicking on “Set Up”.

After that, under “Marketing API” select “Tools”, set “Token Permission” and click on “Get Token”. Copy this access token and keep it safe.

Next, Go to https://developers.facebook.com/tools/debug/accesstoken to increase the expiry time. Paste the access token you got from the previous step and click on “Debug”.

Then confirm the API version and note it down. With this step, the access token will be valid for 2 months and one can extract data (campaign, ads, and account details) from facebook using that access token and Facebook Marketing API for 3 months.

How to get Ad Account ID?

To get a Facebook Ads Account ID. Go to Facebook Manager Account, select the ad account and you will see “act=**************” in the URL.

How to get an App ID and Secret?

Under Google Developer APP selects the app and go to “Setting” > “Basic” and you will get the APP Id and App Secret.

If you want I can help you out with a python code to generate an access token and the entire process automated. Get in touch with me. This all can be done in 1 hour for you.

3. First Facebook API Call:

There are two ways to do that: Python Packages and REST API calls.

Using facebook_business API Python Packages

Create a “test.py” to confirm the working Facebook API.

#!/usr/bin/python3
 
import sys
#optional
sys.path.append('/opt/homebrew/lib/python3.7.4/site-packages') 
# Replace this with the place you installed facebookbusiness using pip
sys.path.append('/opt/homebrew/lib/python3.7.4/site-packages/facebook_business-3.0.0-py3.7.4.egg-info') 
# same as above
#main code start from here
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
 
my_app_id = 'replace with App id'
my_app_secret = 'replace with App secret'
my_access_token = 'replace with your Access Token'
 
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
my_account = AdAccount('act_<replace with ad account id>')
 
campaigns = my_account.get_campaigns(fields=['id','name'])
print("\ncampaign :",campaigns)

Run the above python code. You will see a list of all the campaigns created in your Ad Account. You should see an output similar to the below screengrab.

Using REST API call

Another method of getting campaign insight using Facebook API call is HTTP/Rest/URL call.

#!/usr/bin/python3
import requests
import json
 
ver = "v7.0"
account = 'act_<replace with ad account id>'
access_token = 'replace with access token'
insights = 'reach,impressions,clicks,campaign_id,spend,conversions,objective'
url = "https://graph.facebook.com/"+ver+"/"+account+"/campaigns?fields=name,status,objective,insights{"+insights+"}&access_token="+access_token
 
headers = {}
contact_list = []
 
r = requests.get(url = url, headers = headers)
response_dict = json.loads(r.text)
#print(response_dict)
 
for campaign in response_dict['data']:
    print("\n campaign :",campaign) 

Run the above file you will see something like this.

Note:

  1. ver = Version. Remember I asked to save the version somewhere in step 2 above.
  2. You Don’t have it! no worry go to Facebook Apps, select your “app” navigate to “Setting “ >  “Advanced” > “upgrade API version” and you will get the API version used.

Congratulation! you have successfully set up Facebook API. Now, here’s my guide to help you in developing a complete Python code for extracting campaign data using Facebook API with proper code structuring. End-To-End Facebook Campaign Data Extraction Programmatic Solution.

Hope I was able to solve the problem. If you like this article and think it was easy to understand do share it with your friends and connection. Thank you! see you soon.

For any suggestions or doubts ~ Get In Touch

  • Instagram
  • Linkedin

Checkout out my other API Integration and Coding Solution Guide

Share this post: on Twitter on Facebook on Google+ on LinkedIn

  • Tags Facebook api, Facebook API setup, facebook developer account setup, Facebook marketing API, Facebook marketing API setup
JeevanGupta
Technical, Travel and Social
  • API Setup
  • Python3
  • Web Devop
  • Social
  • ANN
About Us
  • Connect with Jeevan Gupta
  • Work with Jeevan
  • About Jeevan Gupta
  • Privacy Policy
  • Terms of Use
© 2023  Jeevan Gupta - Have a lovely day! Privacy Policy