Our organization has two Facebook ad accounts. I want to retrieve marketing insights data (impressions, clicks, spends, etc.) for each ad account weekly and load the data into our warehouse for reporting.
My understanding is that in order to access such data, I will have to create a Facebook App which I did. After creating the App, I added the product Marketing API to the App. I then went ahead linked the two AD accounts to the Marketing API product (I did this by going to the App's dashboard, selecting the Marketing API product under the products section on the left navigation pane, and clicking on settings).
Next, using the Graph API Explorer, I generated an access token for the app and attempted to run a curl query for retrieving some data.
curl -G \
-d "fields=impressions" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/2.12/9094495844881/insights"
The error I get is:
error":{"message":"Unknown path components:
/9094495844881/insights","type":"OAuthException","code":2500,
"fbtrace_id":"A8oc2/czXYC"}}
What I am doing wrong here? Is the approach wrong to begin with?
You need to generate a user access token or create a system user. This is how Facebook verifies you have permission to make requests. Facebook access docs.
User Access Token
Here is how you generate a user token through Facebook's UI. You need to have access to both ad accounts for this to work.
Generating the token
Obtain an access token using the graph explorer.
Make sure to select your app in the “Application:” drop down on the
top right corner
Click on the "Get Token" drop down then select "Get User Access Token"
Select the following permissions ads_manage and ads_read.
Requesting a long life token
The token you generated will expire in around a hour so you will need to request a long life token.
Copy the token you just generated
Head to the access token debugger
Click "Extend Access Token" at the bottom
Your now ready to make request to the Insight API.
System User
If your company has a business manager set up I would recommend creating a system user.
During the creation process you will get a token which can be used to make requests. Don't forget to assign the ad accounts to the system user through your business manager.
Related
I am integrating facebook leads in my CRM, like I can get leads from Facebook Pages on behalf of my client's.
I am encountering this error.
To subscribe to the leadgen field, one of these permissions is needed: leads_retrieval
I have standard access for 'leads_retrival'. Advanced access can be achieved after hitting the API at least one time.
you need a token that has advanced access to leads_retrival. To get such a token, you can go to the Facebook Graph API Explorer, and then you can add permissions of leads_retrival. Permission can be added in the right middle of the tool and you can add them for testing purposes as you can see in the picture. .
Once you add the permissions, then you can click on generate access token, and a popup will open from Facebook, you have to allow a few things and then you can use that access token for hitting the API for testing purposes. Then you can apply for advanced API access.
I want to write a process which downloads data about my ads.
Basically this is the code:
https://github.com/airflow-plugins/facebook_ads_plugin/blob/master/hooks/facebook_ads_hook.py
It construct a URI as:
https://graph.facebook.com/v{api_version}/act_{account_id}/insights?{payload}
Where payload contains the access_token, breakdowns, fields etc..
Now, I passed over the facebook dev guide https://developers.facebook.com/docs/graph-api/using-graph-api/ and it doesn't explain how do I get the account_id and the token.
It always leads to a user account that needs to log-in to facebook and then process the request.
I want to build a process that doesn't involve user action. Just download logs about my ads.
How can I do that? Where can I get the account_id and the token.
In other systems like google and other they create a json file with credentials that is used for the outh. there doesn't seem to be equivalent with facebook.
For this type of use case, I would create a System User that is permissioned on all your ad accounts, following this guide. That will allow you to generate a token that can used without user login. (You can do the same for your personal facebook account, but that requires you to pass app verification. This can be done without that.)
Then to get the account ids of all the accounts the system user is permissioned on, query the "me/adaccounts" endpoint, using the access token generated for the system user. The docs for that are here. You can use that to get a list of all the account ids.
Alternatively, if you only need one account id, you can get that straight from the facebook ads manager.
I try to get ads performance data using this api: https://graph.facebook.com/v2.5/{ad-account-id}/insights. User access token is needed when doing this request. I have several ad accounts. But one user access token is valid just for part of my ads accounts. The error message is "(#10) You do not have sufficient permissions to perform this action". Why? (I can access the dashboard report of all ad accounts.)
Every user access token is related to an app. I have two user access tokens which are generated with different apps. If I change to another access tokens, the ad accounts of which I can get performance data by api are different. So I guess this is related to apps by which user access token is generated.
Access tokens are made of up a user + app. Make sure both the user, and the app have access entity you're trying to access (in this case, ad account).
Also, when generating the token make scope includes the permission for ads_read
I am using Facebook as advertising platform to promote my application on Apple and Google stores. I would like to make windows service which will download daily report(s) about advertising status of my marketing campaign running one Facebook, preferably using 60 day token, or some permanent solution so that token is retrieved when required. I think that i understand everything to do this except how to generate access token to use it with Facebook Graph API. Which token for which Facebook account do I need and how to obtain it?
You'll need a Facebook app and to grant that app the ads_read permission in order to retrieve reports about your advertising efforts via the API (source).
You'll want a long-lived token so that you don't have to re-authenticate very often. The access token documentation details the steps to exchange a short-lived token for a long-lived one.
You may also want to consider managing the app, ad account, and access tokens (via a business system user) with the FB Business Manager.
Create a facebook app.
Go to the Graph API Explorer in Facebook's Tools & Support section.
Pick your app from the drop down.
Hit Get Token > Get User Access Token.
In Select Permissions choose required permissions or select them from extended permissions.
Use the user access token that will be presented to you in the access token form input field.
I'm creating a webpage for a business, I want to pull specific information via facebook (business hours, etc). I'm successfully doing that through Graph, however I have to regenerate a new token because they expire.
https://developers.facebook.com/docs/graph-api
Is there anyway for me to have a token that doesn't expire like an API integrator key would work with other integrations? I don't have an app and I don't want a user to log in, I can use my login creds to authenticate the webpage to access the access code if needed.
All of the information I read is that you have an app that has the user log in and generate an access token on their behalf.
(this is for 100% GET requests, no POST requests)