V2: api call inside for loop throws exception: Forbidden

Highlighted
Occasional Contributor

V2: api call inside for loop throws exception: Forbidden

When calling getContacts inside for loop gives exception, if it is outside loop (for example update a particular contact ) it is working without any error. 

 

I am using V2 API.

 

Below is my code: 

$contacts = $cc->contactService->getContacts($params['token']);
$allCustomers = $db->getAllCustomerFromDB();
for($i=0;$i<count($allCustomers);$i++){
$exist=0;
for($j=0;$j<count($contacts->results);$j++){
	if($contacts->results[$j]->email_addresses[0]->email_address==$allCustomers[$i]['email']){
		$exist=1;
	}			
}
$contactItem = $cc->contactService->getContacts($params['token'], array("email" => $allCustomers[$i]['email'])); // this makes exception (line number:117)
if($exist==1){
	if(!empty($contactItem->results)){
	$contact = $contactItem->results[0];
	if ($contact instanceof Contact) {
             $contact->addList('20');
             $contact->first_name = $allCustomers[$i]['first_name'];
	     $contact->last_name = $allCustomers[$i]['last_name'];
             $cStat = $cc->contactService->updateContact($params['token'], $contact);
	}
	}
}else{
   $contact = new Contact();
	$contact->addEmail($allCustomers[$i]['email']);
	$contact->addList('10');
	$contact->first_name = $allCustomers[$i]['first_name'];
	$contact->last_name = $allCustomers[$i]['last_name'];
	$cStat = $cc->contactService->addContact($params['token'], $contact);
}
}

The error I got is :

stdClass Object ( [response] => [exception] => stdClass Object ( [xdebug_message] => ( ! ) Ctct\Exceptions\CtctException: Forbidden in F:\websites\cc\src\Ctct\Services\BaseService.php on line 78 Call Stack #TimeMemoryFunctionLocation 10.0014415832{main}( )...\cc_v2.php:0 29.66032289384Ctct\Services\ContactService->getContacts( )...\cc_v2.php:117 ) )

 

3 REPLIES 3
Highlighted
Moderator

Re: V2: api call inside for loop throws exception: Forbidden

Hello,

 

Thank you for reaching out to Constant Contact API Developer Support.

 

Would you be willing to try putting your calls to create and/or update contacts inside of a try statement that will catch the exception and then give us the error response message? That may give us more detail about why you are seeing that response.

 

Example:

 

try {
    $$cStat = $cc->contactService->addContact($params['token'], $contact);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }
}


Regards,

 

David B.
Tier II API Support Engineer

Highlighted
Occasional Contributor

Re: V2: api call inside for loop throws exception: Forbidden

I tried using try catch also.

Exception I got is I mentioned in my post already.

same error.

Highlighted
Moderator

Re: V2: api call inside for loop throws exception: Forbidden

Hello,

 

There are a variety of reasons you could get a Forbidden response. Examples would be an invalid or missing API key, an inactive API key, invalid or missing authorization header, the access token was revoked, the user account associated with the access token does not have the necessary privileges (e.g. Campaign Creator user role does not have permission for contacts features), or if you are trying to re-add an unsubscribed contact as action_by_owner.

 

The error response message will contain the details of the error. For example:

"error_message": "The contact can not be opted in using 'ACTION_BY_OWNER'."

Regards,

 

David B.
Tier II API Support Engineer

 

 

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Register for our "Making Sense of Online Marketing for Education" Webinar

Join us on Thursday, August 27th at 2pm ET for a free one-hour webinar where we will be covering how to incorporate online marketing into your educational marketing strategy.

Read More
Featured