How to get Facebook Ads Account Details using python and Facebook Access Token?
Hi Everyone, I hope you are keeping well. Thank you so much for stopping by. Today, we will see how to develop a python code to extract Facebook Ads Account details or Facebook Ads Profile (eg: Ads Account ID, Account Name, Currency Code, etc)using Facebook API (Facebook Marketing API) and Python. In this article, I will be focusing more on extracting Facebook Ads Account ID along with other Facebook Ads Account details. Since the account Id is essential to extract Facebook Campaign data using Facebook API and Python.
Don’t worry you can use this code and learn from this article to extract Facebook Ads Account details to meet your requirements.
To get Facebook Ads Account details, especially the Account ID, there are 2 ways. First, directly get it from the Facebook Ads Manager, second use python code and Facebook API (to be specific Facebook Marketing API) to get the Facebook Ads Account ID along with the rest of the Facebook Ads Account details.
Resource for using Facebook API :
Let’s get started …
Method 1: Getting Ads Account ID from Facebook Campaign Manager
First login to Facebook Manager Account. On successful login, you will see a list of Facebook Ads Accounts you can manage.
So initially you will see the account Facebook ads account of the login user (if any) or the Ads account that is shared with the login user.
You can get the account id of the Facebook Ads to account from the side of the account selection (i.e Campaigns) dropdown. You can also get the account id from the URL as well.
Method 2: Python code to get Facebook Ad Account Details
In this method, we will build python logic to get Facebook Ads Account ID and other details (like location, currency, etc). Python code will be using the Facebook API. Hence it’s important you have completed the Facebook API setup steps and you have all the tokens to create the below credential file.
2.1- Create a JSON file to store Tokens
Before requesting the Facebook Ads Account details using Facebook API we need to OAuth authentication ourselves. To do this easily we will be creating a JSON file, which will store all authentication-related credentials -like Client ID, Client Secret, Access Token, Refresh Token, and Developer Token.
Creating a JSON file to store credentials makes it easy to maintain, update and track credentials as needed. This practice also provides security to your credentials from other team members or one can easily exclude this file from keeping it in the public repository. Proceed by saving the below JSON as “facebook_cred.json”.
{ "access_token":"EAAJZA5tKehAEBAHgIZCFtoYU1gOFkz19zZCKa72FzdQVgKABEK2sfZC9ewiInmTZAtCeoZCPpFgcHIBHC43IZBwPCsMSesFEiE6TIUaSxTxGBXZCAVXwtcEW9e49MlD2O3OAgIEz4ZCBXEIUawVCtZCZAxm9bC7xpAvNWfB5sMdWYCZAumVOQyEKZAEUc9pG8MfhNYuAZD", "app_id":"661797864375297", "app_secret":"d00179ab4d715b9f0abc12c44c6383c5", "account_id":"2242453356063445", "api_version":"v13.0" }
2.2- Building Python Logic to get Account ID
Here, we will first read the credentials from the above JSON file (line number: 46 – 49). Next, we are going to define a function (line number: 9 – 37) to make the Facebook API call and get Ads Account details in the response. The last one calling the above function (line number: 52).
Consider going through the code, and try to get a basic understanding of what’s going on. Don’t forget to save the code file as “facebook_ads_accounts.py”.
#!/usr/bin/python3 import sys import requests import json def get_facebook_ads_account(access_token,api_version): try: url = "https://graph.facebook.com/"+api_version+"/me/adaccounts?fields=name,account_id,currency,timezone_id&access_token="+access_token headers = {} r = requests.get(url = url, headers = headers) fb_ads_account_data = r.json() fb_ads_accounts = fb_ads_account_data['data'] for fb_ads_account in fb_ads_accounts: print("\nfb_ads_account -> ",fb_ads_account) except: print("\nFunction (get_facebook_ads_account) Failed",sys.exc_info()) if __name__ == '__main__': try: print("Facebook Ads Account extraction process Started") #reading client_id json file cred_file = "./facebook_cred.json" facebook_cred = open(cred_file, 'r') cred_json = json.load(facebook_cred) access_token = cred_json["access_token"] app_id = cred_json["app_id"] app_secret = cred_json["app_secret"] api_version = cred_json["api_version"] accounts_details_df = get_facebook_ads_account(access_token,api_version) except: print("\nFacebook Ads Account extraction process Failed",sys.exc_info())
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.
If you have any questions or comments feel free to reach me at ->
Checkout out my other API Integration and Coding Solution Guide