facebook / facebook-python-business-sdk

Python SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
1.31k stars 642 forks source link

Functions does not work #477

Closed paskalnikita closed 6 years ago

paskalnikita commented 6 years ago

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:
Traceback (most recent call last): File "read_objects.py", line 76, in 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!

leobarcellos commented 6 years ago

Are you passing api_version='v3.0' on FacebookAdsAPI.init? This was the problem for me.

paskalnikita commented 6 years ago

Yeah, after changing that line everything ok

Carmezim commented 6 years ago

Adding the api_version='v3.0' didn't solve for me to run Facebook's example.

paskalnikita commented 6 years ago

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

Carmezim commented 6 years ago

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.

paskalnikita commented 6 years ago

@Carmezim did you change api version on management panel to v3.0 ?

Carmezim commented 6 years ago

@paskalnikita It's set. Unless there's somewhere else I need to set it and am missing. image

paskalnikita commented 6 years ago

@Carmezim visit this page https://developers.facebook.com/tools/api_versioning/