Package: facebookmarketing-python
facebookmarketing-pythonfacebookmarketing is an API wrapper for Facebook and Instagram written in Python.Installingpip install facebookmarketing-pythonFacebook UsageClient instantiationfrom facebookmarketing.client import Client

client = Client('APP_ID', 'APP_SECRET', 'v4.0')OAuth 2.0For more information:https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/Get authorization urlurl = client.authorization_url('REDIRECT_URL', 'STATE', ['pages_manage_ads', 'pages_manage_metadata', 'pages_read_engagement', 'leads_retrieval'])Exchange the code for an access tokenresponse = client.exchange_code('REDIRECT_URL', 'CODE')
access_token = response['access_token']Extend a short-lived access token for a long-lived access tokenresponse = client.extend_token(access_token)  # From previous step
access_token = response['access_token']Get app tokenresponse = client.get_app_token()
app_access_token = response['access_token']Inspect a tokenresponse = client.inspect_token(access_token, app_access_token)  # From previous stepSet the access token in the libraryclient.set_access_token(access_token)  # From previous stepUserFor more information:https://developers.facebook.com/docs/graph-api/reference/user/Get account informationresponse = client.get_account()Get account pagesresponse = client.get_pages()Get page tokenpage_access_token = client.get_page_token('PAGE_ID')  # From previous stepPageFor more information:https://developers.facebook.com/docs/graph-api/reference/page/Get lead generation forms given the pageresponse = client.get_ad_account_leadgen_forms('PAGE_ID', page_access_token)  # From previous stepGet leads info given the lead generation formresponse = client.get_ad_leads('LEADGEN_FORM_ID')Get a sigle lead inforesponse = client.get_leadgen('LEADGEN_ID')WebhooksFor more information:https://developers.facebook.com/docs/graph-api/webhooksThe following methods cover Step 2 and 4 of the Webhook lead retrieval guide:
Webhooks:https://developers.facebook.com/docs/marketing-api/guides/lead-ads/retrieving/Create a webhook for leads retrievalresponse = client.create_app_subscriptions('page', 'callback_url', 'leadgen', 'abc123', app_access_token)  # You get app_access_token from get_app_token() method

response = client.create_page_subscribed_apps('PAGE_ID', page_access_token, params={'subscribed_fields': 'leadgen'})  # You get page_access_token from get_page_token() methodInstagram UsageClient instantiationfrom facebookmarketing.client import Client

client = Client('APP_ID', 'APP_SECRET', 'v12.0')OAuth 2.0For more information:https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/Get authorization urlurl = client.authorization_url('REDIRECT_URL', 'STATE', ['instagram_basic', 'pages_show_list'])Exchange the code for an access tokenresponse = client.exchange_code('REDIRECT_URL', 'CODE')
access_token = response['access_token']PageGet page idresponse = client.get_instagram(page_id, ['instagram_business_account'])
page_id = response['instagram_business_account']['id']MediaGet mediaresponse = client.get_instagram_media(page_id)Get media objectresponse = client.get_instagram_media_object(media_id, fields=['id','media_type','media_url','owner','timestamp'])HashtagSearch hashtagresponse = (client.get_instagram_hashtag_search(page_id, 'coke'))Get hashtag objectresponse = client.get_instagram_hashtag_object(hashtag_id, fields=['id', 'name'])Get hashtag top mediaresponse = client.get_instagram_hashtag_top_media(hashtag_id, instagram_id, ['id','media_type','comments_count','like_count', 'caption'])RequirementsrequestsContributingWe are always grateful for any kind of contribution including but not limited to bug reports, code enhancements, bug fixes, and even functionality suggestions.You can report any bug you find or suggest new functionality with a newissue.If you want to add yourself some functionality to the wrapper:Fork it (https://github.com/GearPlug/facebookmarketing-python)Create your feature branch (git checkout -b my-new-feature)Commit your changes (git commit -am 'Adds my new feature')Push to the branch (git push origin my-new-feature)Create a new Pull Request