UNPKG

facebook-nodejs-business-sdk

Version:
186 lines (169 loc) 5.98 kB
/** * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the license found in the * LICENSE file in the root directory of this source tree. * @flow */ const bizSdk = require('facebook-nodejs-business-sdk'); const process = require('process'); const Business = bizSdk.Business; const ExtendedCredit = bizSdk.ExtendedCredit; const ExtendedCreditAllocationConfig = bizSdk.ExtendedCreditAllocationConfig; const AdAccount = bizSdk.AdAccount; const SystemUser = bizSdk.SystemUser; const AbstractObject = bizSdk.AbstractObject; let app_id = '1224202007596125'; let access_token = 'EAARZAZA73LzF0BACZADSZCYwMsLHUTwAAYKm5Tciz5GZCGM8ZAtqoM12q8ybFt6dpElSqbUuXm77dGmZAeK6r1wygAQZCslDqpsVQY6RQA3WhLirP8BcgoKoCTvlyKzkx6xGPZCcRiDXUleDqSbnAQBd5k0ZBhuLf1AB2VsIS5OlYDoTQlOoRuVpmZAiJY9O6d688sZD'; let app_secret = '709348c9665c33a4f988ff3950098131'; let client_access_token_for_smb_onboarding = 'EAACovfpTQbcBAABhW3FlnxwbgCNF0S4GuXoPM0Gw2AbNE25c7Fq7EB0biSOPfeEPKcViaXNCCVXwHxIHJD07wCflIeQI1ZCW5w5OgRqejAZCCW0fiB0ZAxuoQVT48lsMTnwajQlEfk40eI8r86ctpiJCcsdmoVARXZBwKstERds9eZCCFpX8z'; let partner_bm_id = '249554531892085'; let partner_access_token_for_smb_onboarding = 'EAACovfpTQbcBAOj1jj2SFQO3feGInyhQSIeqTP4mAepTFAZAqMnUF8jFHrE9iTgMqWq3O1erYx6ziUZB6uTeQ8A5Jh4schp7B94s2LiYAWvDdkoGUwtZBmNxPzkuSCFR5dXu5tSiHLVcJNAJ9TkQ6HYfy7AiPcRZAMB877SsudiBblUFH3ZAX'; let extended_credit_line_id = '379182178863816'; let app_id_for_onboarding = '185533902045623'; let page_id = '190446001681363'; const api = bizSdk.FacebookAdsApi.init( client_access_token_for_smb_onboarding ); const showDebugingInfo = true; // Setting this to true shows more debugging info. if (showDebugingInfo) { api.setDebug(true); } const logApiCallResult = (apiCallName, data) => { console.log(apiCallName); if (showDebugingInfo) { console.log('Data:' + JSON.stringify(data)); } }; let fields, params; void async function() { try { // _DOC oncall [business_api] // _DOC open [SMB_PARTNER_ONBOARDING_GEN] // _DOC vars [access_token, client_access_token_for_smb_onboarding, partner_bm_id, partner_access_token_for_smb_onboarding, extended_credit_line_id, app_id_for_onboarding, page_id, partner_bm_id] // Step 1. Use an Advertiser’s access token to generate a Child Business Manager. // This can be fetched by integrating the Facebook Login button on // your webpage and fetching the Advertiser’s access token after // the login. The Advertiser does not have access to the BM by default. // The Partner has root access to Child BM. fields = [ ]; params = { 'name' : 'Advertisers child BM', 'vertical' : 'OTHER', 'page_permitted_roles' : ['ADVERTISER'], 'timezone_id' : '1', }; let child_business = await (new Business(partner_bm_id)).createOwnedBusiness( fields, params ); let child_business_id = child_business.id; // Step 2. Share a Line of Credit from a Partner’s Parent BM to Child // BM (Advertiser’s BM). This uses the Partner’s System User // access token. bizSdk.FacebookAdsApi.init(partner_access_token_for_smb_onboarding); fields = [ ]; params = { 'receiving_business_id' : child_business_id, 'amount' : '10', }; let extended_credit = await (new ExtendedCredit(extended_credit_line_id)).createOwningCreditAllocationConfig( fields, params ); // Step 3. Create an access token of the System User under the Child BM. // This uses the Partner’s access token. bizSdk.FacebookAdsApi.init(partner_access_token_for_smb_onboarding); fields = [ ]; params = { 'app_id' : app_id_for_onboarding, 'scope' : 'manage_pages,ads_management,business_management', }; let raw_child_business_token = await (new Business(child_business_id)).createAccessToken( fields, params ); let child_business_token = raw_child_business_token.access_token; // Step 4. Fetch the funding source shared with the [Child or Parent] BM. This funding source is used to // create the Ad account below. // This uses the Child BM’s access token. bizSdk.FacebookAdsApi.init(child_business_token); fields = [ 'id', 'allocated_amount', ]; params = { }; let responses = await (new Business(child_business_id)).getExtendedCredits( fields, params ); let response_id = responses[0].id; // Step 5. Create an Ad Account under the new Child BM. // This uses the Child BM’s System User access token. // The System User for their Child BM is the owner of the Ad Account. fields = [ ]; params = { 'name' : 'Advertisers Ad Account', 'currency' : 'USD', 'timezone_id' : '1', 'end_advertiser' : page_id, 'media_agency' : 'NONE', 'partner' : 'NONE', 'funding_id' : response_id, }; let account = await (new Business(child_business_id)).createAdAccount( fields, params ); let account_id = account.id; // Step 6. Fetch the System User for the Client business. This uses the Child // BM’s access token. This System User ID is used in the Api calls below. bizSdk.FacebookAdsApi.init(child_business_token); fields = [ ]; params = { }; let system_users = await (new Business(child_business_id)).getSystemUsers( fields, params ); let system_user_id = system_users[0].id; // Step 7. Add a System User as an Admin to the Ad Account. // This uses the Child BM’s access token. bizSdk.FacebookAdsApi.init(child_business_token); fields = [ ]; params = { 'user' : system_user_id, 'role' : 'ADMIN', 'business' : child_business_id, }; let assigned_user = await (new AdAccount(account_id)).createAssignedUser( fields, params ); // [CLEAN UP] Delete the Child Business Manager we just created. // One Client can only have one Child BM under your Parent Business Manager. // This step uses the Partner's access token. bizSdk.FacebookAdsApi.init(partner_access_token_for_smb_onboarding); fields = [ ]; params = { 'client_id' : child_business_id, }; await (new Business(partner_bm_id)).deleteOwnedBusinesses( params ); // _DOC close [SMB_PARTNER_ONBOARDING_GEN] } catch(error) { console.log(error); process.exit(1); } }();