Unable to access Facebook Marketing API - facebook

I am trying to access Facebook Marketing API. I have created a facebook app and I can get the token from the user(in this case it is me). But when I use it in the Python Facebook Ads SDK. I get this error :
"message": "(#273) This Ads API call requires the user to be admin of the ad account. User is not admin on ad account <account ID>.
The account is of a client but I am also an admin there.
I have set "Ads API Account Configuration" in Marketing API settings and "Authorized Ad Account IDs" in app's advanced settings. I have tried in Graph API and I am getting same issue. I was able to access it yesterday but it stopped working and I have tried with multiple apps but the issue is same.
The code I am trying is:
import os
from facebookads import FacebookAdsApi
from facebookads import objects
app_id = os.getenv("APP_ID")
app_secret = os.getenv("APP_SECRET")
access_token = data['accessToken'] # data is a dictionary with accessToken
FacebookAdsApi.init(app_id, app_secret, access_token)
me = objects.AdUser(fbid='me')
my_accounts = list(me.get_ad_accounts())
print(my_accounts)
account = my_accounts[0]
account.remote_read(fields=[objects.AdAccount.Field.amount_spent])

I was getting the error because my Facebook ads payment failed.

Related

How can I get an instagram business account with Facebook Graph API?

The condition I am using:
Graph API version: 7.0
The problem I have: I can get the Instagram business account of my Facebook developer account,
However, I cannot get the Instagram business account when I login Facebook with another account.
The account has the permissions below:
The Instagram account is connected to its Facebook page.
The Instagram account is a business profile.
The Facebook account that I want to connect to Instagram account has the authorisation more than editor.
I could get this:
/me?fields=name,picture, but not
/me/accounts?fields=instagram_business_account
I tried with this API:
/me/accounts?fields=instagram_business_account{name,username,profile_picture_url},access_token,name,picture{url}', function(response) { }
The response I got:
{ "data": [ ] }
Do I need additional authorisation or something?
Your App is most likely not public and and you need to go through the review process first - with all the permissions you need. Else, it will only work for users with a role in the App.
More information: https://developers.facebook.com/docs/apps/review/
Also, the IG account must be a business account, and it must be connected to the Facebook Page: https://www.facebook.com/business/help/898752960195806
To get Instagram IDs linked to user's pages you need these permissions (verified and approved for your app): instagram_basic, pages_show_list, pages_read_engagement.
You can read guide here https://developers.facebook.com/docs/instagram-api/getting-started
Everything is correct on that page except extra permission needed to get IG ID: pages_read_engagement

Get group events as a system user with Facebook API

I have a website which should list all the events from my facebook group. So I created a facebook dev account and created an app.
When I tried to get the data with the graph API explorer it worked, but the access token expired after a few hours. So I had to get a never-expiring access token.
I created a sytem user at the facebook business manager. With the system user I got a never-expiring access token but I couldn't get the event data anymore. Everytime I tried it I got this response.
"Unsupported get request. Object with ID 'xxxxxxxxxxxxx' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
I have the following permissions as system user:
read_insights, read_audience_network_insights, manage_pages, pages_manage_cta, pages_show_list, publish_pages, business_management
Why did it fail? Are the permissions correct?
Thanks for your help.

Can't get data from Facebook Marketing API insights - error #273

I'm trying to get the data from Facebook Marketing API insights and receiving the following message: (#273) This Ads API call requires the user to be admin of the ad account. User is not admin on ad account XXXXXXXXXXXXXXXXX.
I did all the things, described in the documentation:
- created the app
- in the Advanced settings -> Advertising Accounts added client's Ad Account
- added Marketing API into the Products section
- generated Access token with the ads_read, read_insights, even ads_management permissions
The client's Account ID is added to Marketing API -> Settings -> Ads API Account Configuration.
My account has Administrator role in the application.
What can cause the problem?

How to get users' FB Ad Account Id via API?

I can get my own ad account id via a curl requests to
https://graph.facebook.com/v2.8/me?fields=id%2Cname%2Cadaccounts&access_token=
We have Ads Management Basic Access permissions from Facebook.
I have also tried https://developers.facebook.com/docs/graph-api/reference/user/adaccounts/
Both get the same error.
{ error:
{ message: '(#3) Application does not have the capability to make this API call.',
type: 'OAuthException',
code: 3,
fbtrace_id: 'HiXZVS8UeI8' } }
What is the endpoint to get user Ad accounts? What permissions do I need?
The page you link to lists
Managing advertisements requires an access token with the extended
permission for ads_management
You can test in the Graph API Console when you generate a new token

Accessing Facebook Marketing API

I'm trying to create a Facebook ad using the Facebook Marketing API. I haven't used it before and was wondering how I gain access to the API.
I have an access token with ads_management permission but are any other steps necessary and how would I then access the API?
Thank you for your help.
Along with a access token with the appropriate scope you need a ad account and a Facebook application and to authorize the account as an advertiser for the app. Add the account under Advertising Accounts within Advanced settings of your app. See also the Facebook documentation page on Marketing API access at https://developers.facebook.com/docs/marketing-api/access. For now you should follow the Development access level.
To get start with the API you'll need to know your Ad Account ID which you should have from the prior step. If not you can find it from https://www.facebook.com/ads/manager
A simple example of using the API is to make a cURL request for the id and name of your ad account. Replace with your token and with the number ID retrieved from the above step.
curl -G \
-d "fields=name" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/v2.4/act_<AD_ACCOUNT_ID>/
For further API documentation see the getting started guide at https://developers.facebook.com/docs/marketing-api/getting-started and also the API overview at https://developers.facebook.com/docs/marketing-api/using-the-api.
If you talk Python, this might help. Facebook offers a business SDK that abstracts the marketing API, and that way you can access the marketing api with the facebook sdk for python. Do "pip install facebook-business", provide the creadentials and the ads account_id. You are ready to go.
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
my_app_id = ''
my_app_secret = ''
my_access_token = ''
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
my_account = AdAccount('act_XXXXX')
This will help you get started:
https://developers.facebook.com/docs/business-sdk/getting-started/
https://developers.facebook.com/docs/marketing-api/sdks/