Skip to content

Instantly share code, notes, and snippets.

@tzookb
Created December 20, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzookb/55f0e1a9cada58412cf673f10ad99767 to your computer and use it in GitHub Desktop.
Save tzookb/55f0e1a9cada58412cf673f10ad99767 to your computer and use it in GitHub Desktop.
fb marketing api - agency
def claim_page_to_business(page: Page, business: Business):
"""
We claim the page into the business.
The business is set as agency for the page.
In case the page is already claimed, we will skip the error,
as the result we want to achieve is already set.
"""
try:
# this adds the business through the page
params = {
"business": business["id"],
"permitted_tasks": [
Business.PermittedTasks.advertise,
Business.PermittedTasks.analyze,
# Business.PermittedTasks.
]
}
res = page.create_agency(params=params)
# I worked with the code below, but it didnt work with other users except me.
# this add the page through the business
# res = business.create_client_page(
# None,
# {
# "page_id": page["id"],
# "permitted_tasks": [
# Business.PermittedTasks.advertise,
# Business.PermittedTasks.analyze,
# ],
# },
# )
except FacebookRequestError as e:
if is_page_already_claimed_by_business(e):
return True
raise e
def add_system_user_to_page_as_advertiser(page: Page):
"""
Adding our system user to be able to advertise on behalf of the page.
"""
system_user_id = get_system_user_id()
page.create_assigned_user(
None, {"user": system_user_id, "tasks": [Page.PermittedTasks.advertise]}
)
def add_page_system_user(access_token):
fb_api = get_facebook_api(access_token)
page = Page("me", None, fb_api)
page.api_get(["id"])
business_id = get_business_id()
business = Business(business_id, None, fb_api)
claim_page_to_business(page, business)
add_system_user_to_page_as_advertiser(page)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment