facebook / facebook-nodejs-business-sdk

Node.js SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
494 stars 227 forks source link

Marketing API Sample Example does not work #86

Closed lanbau closed 5 years ago

lanbau commented 5 years ago

This is the sample code generated from the wizard with no modifications.

Does not work at all.

https://developers.facebook.com/ads/blog/post/2017/09/12/autogen-node.js-sdk/

const adsSdk = require('facebook-nodejs-ads-sdk');

i believe this package is outdated.. please update with a business npm package example. Code Samples

product_catalog =  (new Business(business_id)).createProductCatalog(
                                               ^

TypeError: (intermediate value).createProductCatalog is not a function
    at Object.<anonymous> (/latest/AdsSample/SAMPLE_CODE.js:68:48)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
lanbau commented 5 years ago
Removed all tokens for security purposes.

/**
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 adsSdk = require('facebook-nodejs-ads-sdk');
const Business = adsSdk.Business;
const ProductCatalog = adsSdk.ProductCatalog;
const ProductFeed = adsSdk.ProductFeed;
const ProductSet = adsSdk.ProductSet;
const ExternalEventSource = adsSdk.ExternalEventSource;
const AdAccount = adsSdk.AdAccount;
const Campaign = adsSdk.Campaign;
const AdSet = adsSdk.AdSet;
const AdCreative = adsSdk.AdCreative;
const Ad = adsSdk.Ad;
const AdPreview = adsSdk.AdPreview;

let access_token = '';
let app_secret = '';
let ad_account_id = '';
let business_id = '';
let page_id = '';
let pixel_id = '';
let app_id = '';
const api = adsSdk.FacebookAdsApi.init(access_token);
const account = new AdAccount(ad_account_id);
const showDebugingInfo = true; // Setting this to true shows more debugging info.
if (showDebugingInfo) {
api.setDebug(true);
}

let product_catalog;
let product_catalog_id;
let product_feed;
let product_feed_id;
let product_set;
let product_set_id;
let external_event_source;
let external_event_source_id;
let campaign;
let campaign_id;
let ad_set;
let ad_set_id;
let creative;
let creative_id;
let ad;
let ad_id;
let adpreview;
let adpreview_id;

const logApiCallResult = (apiCallName, data) => {
console.log(apiCallName);
if (showDebugingInfo) {
console.log('Data:' + JSON.stringify(data));
}
};

const fields = [
];
const params = {
'name' : 'Test Catalog',
};
product_catalog =  (new Business(business_id)).createProductCatalog(
fields,
params

);
product_catalog
.then((result) => {
logApiCallResult('product_catalog api call complete.', result);
product_catalog_id = result.id;
const fields = [
];
const params = {
'name' : 'Test Feed',
'schedule' : {'interval':'DAILY','url':'https://developers.facebook.com/resources/dpa_product_catalog_sample_feed.csv','hour':'22'},
};
return (new ProductCatalog(product_catalog_id)).createProductFeed(
fields,
params
);
})
.then((result) => {
logApiCallResult('product_feed api call complete.', result);
product_feed_id = result.id;
const fields = [
];
const params = {
'name' : 'All Product',
};
return (new ProductCatalog(product_catalog_id)).createProductSet(
fields,
params
);
})
.then((result) => {
logApiCallResult('product_set api call complete.', result);
product_set_id = result.id;
const fields = [
];
const params = {
'external_event_sources' : [pixel_id],
};
return (new ProductCatalog(product_catalog_id)).createExternalEventSource(
fields,
params
);
})
.then((result) => {
logApiCallResult('external_event_source api call complete.', result);
external_event_source_id = result.id;
const fields = [
];
const params = {
'objective' : 'PRODUCT_CATALOG_SALES',
'promoted_object' : {'product_catalog_id':product_catalog_id},
'status' : 'PAUSED',
'name' : 'My Campaign',
};
return (new AdAccount(ad_account_id)).createCampaign(
fields,
params
);
})
.then((result) => {
logApiCallResult('campaign api call complete.', result);
campaign_id = result.id;
const fields = [
];
const params = {
'status' : 'PAUSED',
'targeting' : {'geo_locations':{'countries':['US']}},
'daily_budget' : '1000',
'billing_event' : 'IMPRESSIONS',
'bid_amount' : '20',
'campaign_id' : campaign_id,
'optimization_goal' : 'OFFSITE_CONVERSIONS',
'promoted_object' : {'product_set_id': product_set_id},
'name' : 'My AdSet',
};
return (new AdAccount(ad_account_id)).createAdSet(
fields,
params
);
})
.then((result) => {
logApiCallResult('ad_set api call complete.', result);
ad_set_id = result.id;
const fields = [
];
const params = {
'url_tags' : 'utm_source=facebook',
'object_story_spec' : {'page_id': page_id, 'template_data': {'call_to_action': {'type': 'SHOP_NOW'}, 'link': 'www.example.com', 'name': '{{product.name}} - {{product.price}}', 'description': '{{product.description}}', 'message': '{{product.name | titleize}}'}},
'name' : 'My Creative',
'product_set_id' : product_set_id,
'applink_treatment' : 'web_only',
};
return (new AdAccount(ad_account_id)).createAdCreative(
fields,
params
);
})
.then((result) => {
logApiCallResult('creative api call complete.', result);
creative_id = result.id;
const fields = [
];
const params = {
'tracking_specs' : [ {'action_type': ['offsite_conversion'], 'fb_pixel': [pixel_id]} ],
'status' : 'PAUSED',
'adset_id' : ad_set_id,
'name' : 'My Ad',
'creative' : {'creative_id':creative_id},
};
return (new AdAccount(ad_account_id)).createAd(
fields,
params
);
})
.then((result) => {
logApiCallResult('ad api call complete.', result);
ad_id = result.id;
const fields = [
];
const params = {
'ad_format' : 'DESKTOP_FEED_STANDARD',
};
return (new Ad(ad_id)).getPreviews(
fields,
params
);
})
.then((result) => {
logApiCallResult('adpreview api call complete.', result);
adpreview_id = result[0].id;
})
.catch((error) => {
console.log(error);
});
jingping2015 commented 5 years ago

We will update our sample code ASAP. Please use const adsSdk = require('facebook-nodejs-business-sdk'); to import the package. Please update to the latest package. Let's know if you still have issue.

lanbau commented 5 years ago

Hi @jingping2015 thanks for replying!

I tried this example at the following link https://developers.facebook.com/docs/business-sdk/getting-started/#js

Here are the steps to reproduce

npm init --yes 
touch index.js
copied & pasted sample code
changed access tokens & account id
npm install
node index.js

Problem got the following error (removed my details)

{ Error
    at FacebookRequestError.FacebookError [as constructor] 
    at runCallback (timers.js:694:18)
    at tryOnImmediate (timers.js:665:5)
    at processImmediate (timers.js:647:5)
  name: 'FacebookRequestError',
  message: '(#2) Service temporarily unavailable',
  stack:
   'Error\n    at FacebookRequestError.FacebookError [as constructor] ,
  status: 400,
  response:
   { error:
      { message: '(#2) Service temporarily unavailable',
        type: 'OAuthException',
        is_transient: true,
        code: 2,
        fbtrace_id: '' } },
  method: 'GET',
  url:
   'https://graph.facebook.com/v3.2/...',
  data: {} }
martinagius commented 5 years ago

I also get Service temporarily unavailable..

Is the APP supposed to generate new tokens using this method?

https://developers.facebook.com/docs/facebook-login/access-tokens

jingping2015 commented 5 years ago

Hey, if you got a Service temporarily unavailable error, this is actually an api issue. Please report the issue on Devsite Bug Report channel: https://developers.facebook.com/support/bugs/ Thanks!

lanbau commented 5 years ago

Amazingly, i was redirected by here https://developers.facebook.com/support/bugs/911656522361899/?disable_redirect=0

Thanks Facebook.