Closed paskalnikita closed 6 years ago
Are you passing api_version='v3.0' on FacebookAdsAPI.init? This was the problem for me.
Yeah, after changing that line everything ok
Adding the api_version='v3.0'
didn't solve for me to run Facebook's example.
Use this:
config_file = open('jsons/config.json')
config = json.load(config_file)
config_file.close()
session = FacebookSession(
config['app_id'],
config['app_secret'],
config['access_token'],
)
api = FacebookAdsApi(session)
FacebookAdsApi.init(session.app_id, session.app_secret, session.access_token, api_version='v3.0')
Update api verion from using manager on fb page
Hey @paskalnikita, thanks a lot for the response.
This is my current code and unfortunately for some reason FB is still giving that error and my package installed is v3.0.1
:
from facebook_business import FacebookSession
from facebook_business import FacebookAdsApi
from facebook_business.adobjects.campaign import Campaign as AdCampaign
from facebook_business.adobjects.adaccountuser import AdAccountUser as AdUser
import json
import os
import pprint
pp = pprint.PrettyPrinter(indent=4)
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')
config_file = open(config_filename)
config = json.load(config_file)
config_file.close()
### Setup session and api objects
session = FacebookSession(
config['app_id'],
config['app_secret'],
config['access_token'],
)
api = FacebookAdsApi(session)
FacebookAdsApi.init(session.app_id, session.app_secret, session.access_token, api_version='v3.0')
if __name__ == '__main__':
FacebookAdsApi.set_default_api(api)
print('\n\n\n********** Reading objects example. **********\n')
### Setup user and read the object from the server
me = AdUser(fbid='me')
### Read user permissions
print('>>> Reading permissions field of user:')
pp.pprint(me.remote_read(fields=[AdUser.Field.permissions]))
### Get first account connected to the user
my_account = me.get_ad_account()
### Read connections (in this case, the accounts connected to me)
# Pro tip: Use list(me.get_ad_accounts()) to make a list out of
# all the elements out of the iterator
my_accounts_iterator = me.get_ad_accounts()
print('>>> Reading accounts associated with user')
for account in my_accounts_iterator:
pp.pprint(account)
print(">>> Campaign Stats")
for campaign in my_account.get_ad_campaigns(fields=[AdCampaign.Field.name]):
for stat in campaign.get_stats(fields=[
'impressions',
'clicks',
'spent',
'unique_clicks',
'actions',
]):
print(campaign[campaign.Field.name])
for statfield in stat:
print("\t%s:\t\t%s" % (statfield, stat[statfield]))
My config.json
is correct as I can run other code to make requests.
@Carmezim did you change api version on management panel to v3.0 ?
@paskalnikita It's set. Unless there's somewhere else I need to set it and am missing.
@Carmezim visit this page https://developers.facebook.com/tools/api_versioning/
Hello! I copied repo to my computer. And tried to run examples/read_objects.py(also I added config.json file). I works fine till line 76: for campaign in my_account.get_ad_campaigns(fields=[AdCampaign.Field.name]): I get an error:
for campaign in my_account.get_ad_campaigns(fields=[AdCampaign.Field.name]):
AttributeError: 'AdAccount' object has no attribute 'get_ad_campaigns'
Your exmaple does not work! How can i fix it?
PS. I install new 3.0 version using pip, calling API_VERSION returns me v2.11
Hope, you can fix this problem!
Traceback (most recent call last): File "read_objects.py", line 76, in