0
votes

I'm trying to create an ad using the Facebook Ads API. So, far I've already created the Ad Set, Ad Creative and Campaign but, when I use the following code to create an ad group, I'm receiving Invalid Parameter exception. Please suggest me if I've any error in the following code.

$campaignId = "xxx";
$appId= "xx";
$ci = "xxx";
$accountId = "xxx";
$appSecret= "xxx";
$accessToken= "xxx";

Api::init($appId, $appSecret, $accessToken);
$api = Api::instance();

$adgroup = new AdGroup(null, $accountId);
$adgroup->setData(array(
  AdGroupFields::CREATIVE => 
    array('creative_id' => $ci),
  AdGroupFields::NAME => 'My First AdGroup',
  AdGroupFields::CAMPAIGN_ID => $campaignId,
  AdGroupFields::ADGROUP_STATUS => AdGroup::STATUS_PAUSED,
));

$adgroup->create();

The error I receive:

Fatal error: Uncaught exception 'FacebookAds\Http\Exception\AuthorizationException' with message 'Invalid parameter' in /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php:137 Stack trace: #0 /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Client.php(216): FacebookAds\Http\Exception\RequestException::create(Array, 500) #1 /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Request.php(282): FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request)) #2 /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(152): FacebookAds\Http\Request->execute() #3 /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(194): FacebookAds\Api->executeRequest(Object(FacebookAds\Http\Request)) #4 /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Object/AbstractCrudObject.php(248): FacebookAds\Api->call('/act_1602736963...', 'POST', Array) #5 /var/www/fb/ads.php(46): FacebookAds\Object\AbstractCrudObject->create() #6 {main} thrown in /var/www/fb/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php on line 137

1
What's the full text of the exception you're receiving? What's the full API call being made by your code? - Igy
I tired with the example code that is provided with the SDK but same problem. - shr3jn
What's the full text of the exception returned by Facebook's server? What you pasted above is the stacktrace from your code, but not the full response the server sent - I do see Invalid parameter, but that's a class of exceptions and there should be a more specific error identifying what field or value was incorrect - Igy
I checked the Facebook's response and found out the error is I didn't have a payment method associated with my ad account. Thanks for your help!! " [error_user_msg] => You need to have a valid payment method associated with your ad account before you can create ads." - shr3jn

1 Answers

3
votes

The problem was I didn't had an payment method associated with my account which was causing the error.