Skip to content

Commit 71f25ed

Browse files
John Hefacebook-github-bot
John He
authored andcommitted
2T ThirdPartyAccount - Python SDK
Summary: In order to simplify the usages for the two tier business model, we are creating SDK classes that abstract away complexity of setup in an effort to reduce onboarding friction. This contains both the classes as well as examples on how to use them. The functionalities covers: - creating a child business and associated ad account - creating and sharing a line of credit with the child business - fetching a particular child business account - fetching the list of child business accounts - getting the Facebook page of a child business - token management is abstracted away as there are 3 tokens involved This diff adds in the functionality for Python only. Reference diff: D73810049 Differential Revision: D74041672 fbshipit-source-id: 9e97df3008d0fe1cf17d16c457e5d35cf50f038c
1 parent a0a8824 commit 71f25ed

File tree

8 files changed

+529
-1
lines changed

8 files changed

+529
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## Unreleased
66

7+
### Added
8+
- Added classes for tech providers
9+
-- CRUD operations for ThirdPartyAccountManager
10+
-- basic operations for ThirdPartyAccount
11+
- Added examples for tech providers
12+
713
## v17.0.0
814

915

@@ -127,4 +133,3 @@ All notable changes to this project will be documented in this file.
127133
### Deprecated
128134
- `parent_id` in `AbstractCrudObject`.
129135
- Function `remote_create`, `remote_read`, `remote_update` and `remote_delete` for `AbstractCrudObject`. Check out our [recommended way](https://github.com/facebook/facebook-python-business-sdk#exploring-the-graph) to make API call with python SDK.
130-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
21+
from facebook_business.adobjects.techprovider.thirdpartyaccountmanager import (
22+
CreateAccountInput,
23+
CreditAllocationProps,
24+
PageProps,
25+
ThirdPartyAccountManager,
26+
)
27+
28+
tech_provider_business_id = "<tech_provider_business_id>"
29+
app_id = "<app_id>"
30+
page_id = "<page_id>"
31+
credit_line_id = "<credit_line_id>"
32+
system_account_token = "<system_account_token>"
33+
user_access_token = "<user_access_token>"
34+
35+
create_account_input = CreateAccountInput(
36+
name="<account_name>",
37+
page=PageProps(
38+
id=page_id,
39+
),
40+
credit_allocation=CreditAllocationProps(
41+
credit_line_id=credit_line_id,
42+
currency_amount="<currency_amount>",
43+
currency="USD",
44+
),
45+
user_access_token=user_access_token,
46+
)
47+
48+
third_party_account_manager = ThirdPartyAccountManager(
49+
app_id, tech_provider_business_id, system_account_token
50+
)
51+
third_party_account = third_party_account_manager.create_account(create_account_input)
52+
print(third_party_account)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
21+
from facebook_business.adobjects.techprovider.thirdpartyaccountmanager import (
22+
ThirdPartyAccountManager,
23+
)
24+
25+
tech_provider_business_id = "<tech_provider_business_id>"
26+
app_id = "<app_id>"
27+
system_account_token = "<system_account_token>"
28+
29+
third_party_account_manager = ThirdPartyAccountManager(
30+
app_id, tech_provider_business_id, system_account_token
31+
)
32+
accound_id_to_delete = input("Enter the account id to delete: ")
33+
third_party_account_manager.delete_account(accound_id_to_delete)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
21+
from facebook_business.adobjects.techprovider.thirdpartyaccountmanager import (
22+
ThirdPartyAccountManager,
23+
)
24+
25+
tech_provider_business_id = "<tech_provider_business_id>"
26+
app_id = "<app_id>"
27+
system_account_token = "<system_account_token>"
28+
29+
third_party_account_manager = ThirdPartyAccountManager(
30+
app_id, tech_provider_business_id, system_account_token
31+
)
32+
account_id = input("Enter the account id to get: ")
33+
34+
account = third_party_account_manager.get_account(account_id)
35+
print(account)
36+
37+
page_id = account.get_facebook_page_id()
38+
print(page_id)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
21+
from facebook_business.adobjects.techprovider.thirdpartyaccountmanager import (
22+
ThirdPartyAccountManager,
23+
)
24+
25+
tech_provider_business_id = "<tech_provider_business_id>"
26+
app_id = "<app_id>"
27+
system_account_token = "<system_account_token>"
28+
29+
third_party_account_manager = ThirdPartyAccountManager(
30+
app_id, tech_provider_business_id, system_account_token
31+
)
32+
accounts = third_party_account_manager.list_accounts()
33+
print(accounts)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
from __future__ import annotations
21+
22+
from dataclasses import dataclass
23+
from typing import Optional, TYPE_CHECKING
24+
25+
if TYPE_CHECKING:
26+
from facebook_business.adobjects.techprovider.thirdpartyaccountmanager import (
27+
ThirdPartyAccountManager,
28+
)
29+
30+
from facebook_business.adobjects.adaccount import AdAccount
31+
from facebook_business.adobjects.business import Business
32+
from facebook_business.api import FacebookAdsApi
33+
34+
35+
@dataclass
36+
class ThirdPartyAccountInfo:
37+
account_id: str
38+
name: str
39+
40+
41+
""" ThirdPartyAccount
42+
43+
These are accounts owned by a tech provider's business, representing
44+
a user of the tech provider's application.
45+
46+
This class allows the tech provider to decide which assets are shared from an existing
47+
facebook user's account/business portfolio (which the user has granted the tech provider access to),
48+
and which assets are created and owned by the tech provider. This class allows the Tech Provider
49+
to provide monetization experiences for the third party user using a higher level interface
50+
than the underlying facebook graph apis.
51+
52+
This class simplifies:
53+
1. Creating Assets that are owned by the tech provider (Ad Accounts, Pages, Pixels, etc) .
54+
2. Accessing Assets from a users facebook account or business account (Pages, Pixels, etc).
55+
3. Combining tech provider owned and user owned assets. (ex: a Product Catalog owned by the tech provider using CAPI events from a user owned dataset)
56+
3. Allocating a portion of the tech provider's Credit Line to the account.
57+
4. Access Token Usage and Management
58+
5. Providing Monetization Services to the third party user
59+
60+
All management operations are performed using the ThirdPartyAccountManager (e.g. CRUD operations)
61+
Creation:
62+
- ThirdPartyAccountManager.create_account() must be used to create a new ThirdPartyAccount
63+
Reading:
64+
- ThirdPartyAccountManager.get_account() must be used to load a single ThirdPartyAccount
65+
- ThirdPartyAccountManager.list_accounts() must be used to load a list of ThirdPartyAccountInfo
66+
Deletion:
67+
- ThirdPartyAccountManager.delete_account() must be used to delete an existing ThirdPartyAccount
68+
"""
69+
70+
71+
class ThirdPartyAccount:
72+
73+
def __init__(
74+
self,
75+
account_manager: ThirdPartyAccountManager,
76+
account_id: str,
77+
account_token: Optional[str] = None,
78+
):
79+
# The manager for this account
80+
self.account_manager = account_manager
81+
82+
# The ID of the account
83+
self.account_id = account_id
84+
85+
# The access token for the account
86+
self.account_token = account_token
87+
88+
""" get_facebook_page_id
89+
90+
Return the Facebook Page associated with the account.
91+
92+
This is the Facebook Page that was shared with the account
93+
during creation. If the page was updated, this will return
94+
the updated page.
95+
96+
Returns: Optional[str] - The Facebook Page ID or None if not found
97+
"""
98+
99+
def get_facebook_page_id(self) -> Optional[str]:
100+
api = self.get_api()
101+
try:
102+
business = Business(self.account_id, api=api)
103+
ad_accounts = business.get_owned_ad_accounts(
104+
fields=[AdAccount.Field.end_advertiser]
105+
)
106+
if ad_accounts:
107+
return ad_accounts[0].end_advertiser
108+
except Exception as e:
109+
raise e
110+
print(f"No Facebook page ID found for account - {self.account_id}")
111+
return None
112+
113+
def get_api(self) -> FacebookAdsApi:
114+
return FacebookAdsApi.init(access_token=self.account_token)
115+
116+
def refresh_access_token(self) -> None:
117+
self.account_token = self.account_manager.regenerate_token(self.account_id)

0 commit comments

Comments
 (0)