We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Getting 400 error when adding bulk contacts but not on IO passing same JSON string

SOLVED
Go to solution
TimG1567
Frequent Participant

Getting 400 error when adding bulk contacts but not on IO passing same JSON string

Hi. 

I have successfully been able to clear lists via the API using a python script. However when I go to add contacts to list I get an error in my python script but not when I use the IO site. 

Can anyone help me understand what I am doing wrong? 

Code:

import requests

headers = {

'Authorization': 'Bearer X',
'Content-Type': 'application/json',

}
update_contact = {"lists": [{"id": "1"}],"email_addresses": [{"email_address": "bobrichard.abob19955@gmail.com"}],"first_name": "Regan","last_name": "Mary"}

r_2 = requests.post('https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=X', headers=headers ,params = update_contact)

print(r_2.text)

 

 

Thank you in advance. 

1 ACCEPTED SOLUTION
TimG1567
Frequent Participant

Hi David, 

Unfortunately  I do not have the keys I was using prior to the issue. However I will follow up with you if I receive the same error message again. 
 

Thank you once again for all your help. 

View solution in original post

13 REPLIES 13
David_B.
Moderator

Hello,

 

Thank you for reaching out to the API Support team here at Constant Contact.

 

I believe the issue is that your are submitting the body of your request in the params field instead of the data field. The params field would be used for the information like action_by and api_key instead of including them in your URL.

 

Try updating your request to look like the following:

r_2 = requests.post('https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=X', headers=headers, data=update_contact)

 

Please let me know if you have any other questions!

 

Sincerely,
David Bornstein
Tier II API Support Engineer

TimG1567
Frequent Participant

Hi David, 

Thank you for getting back to me. the answer worked. I had to put the json string in quotes as well and change params to data. 

For my own knowledge, if an email address already exists I need to use PUT to update the contacts list_ID right? 


Is there a way to POST bulk contacts to individual lists without needing the contact ID ? similar to the POST call I made above? 

Thank you in advance. 

 

Hello,

 

Glad to hear that worked!

 

That is correct. If you are working with individual contacts, the suggested method is to first GET by the email address. If there is an existing contact it will return the ID and you would PUT to that contact. Otherwise you would POST a new one.

 

If you are looking to bring in multiple contacts, you can use our bulk activity method. This is a single post call with all email addresses, existing or not. See our documentation on this method for details here: http://developer.constantcontact.com/docs/bulk_activities_api/bulk-activities-import-contacts.html

 

Please have a look and let me know if you have any other questions!

 

Sincerely,
David B.
Tier II API Support Engineer

TimG1567
Frequent Participant

Hi David, 

Thank you for the response. 

 

Could you please help me out with the following? 


When I am trying the bulk contact URL as provided below in the link you sent, I am getting error: 


[{
"error_key":"mashery.not.authorized.inactive",
"error_message":"The Mashery account associated with this request is not active, or an invalid API key was used."
}]

 

Code: 

headers = {

'Authorization': 'x',
'X-Originating-Ip': 'x',
'Content-Type': 'application/json',

}
update_contact = '{"import_data": [{"email_addresses": ["hector@hector.com"],"first_name": "Hector","last_name": "Smith"},{"email_addresses": ["Jane@Doe.com"],"first_name": "Jane","last_name": "Doe"}, {"email_addresses": ["Paul@Patel.com"],"first_name": "Pradeep","last_name": "Patel"}],"lists": ["1"]}'

r_3 = requests.post('https://api.constantcontact.com/v2/activities/addcontacts&api_key=x', headers=headers ,data = update_contact)



Hello,

 

That error should only occur if the API Key you are using is disabled or an invalid key is used. The most common reason that happens is when the API Key and Access Token are mixed up and put in the wrong place.

 

So that I can look into this for you further, please email us at webservices(at)constantcontact.com and include your API Key and if possible this code snippet with your live information.

 

Sincerely,
David B.
Tier II API Support Engineer

TimG1567
Frequent Participant

Hi David, 


I sent an email to the address below. 

Once again thank you. 

TimG1567
Frequent Participant

Hi David, 

I have successfully able to correct my error, the API keys had changed. 

How do other API developers connect into Constant Contact with changing API keys? 

Is there an automated to adjust the API call to include the changed API keys? 

Thank you!

Hello,

 

Glad to hear you were able to get that working. Constant Contact API Keys are static and do not change, so no additional work is needed to change the API Key within your application.

 

Regards,
David Bornstein
Tier II API Support Engineer

TimG1567
Frequent Participant

Hi David, 

I am not sure if the keys are static, because initially I when I used the API keys in my code, I was successfully able to call. 

After two to three weeks I received the error below automatically without me changing any API keys. 

 

' "error_key":"mashery.not.authorized.inactive",
"error_message":"The Mashery account associated with this request is not active, or an invalid API key was used." 


Could you please confirm our account is not being used maliciously if API Key's 
remain static? That is the only reason I can see why API Keys would change. 

Hello,

 

I can assure you that we do not change API Keys. If we ever find an issue with a key being used improperly, we would disable the key or the entire account. You can always see your API Keys and their status here: https://constantcontact.mashery.com/apps/mykeys

 

Do you still have the API Key that you were using previously?


Regards,
David Bornstein
Tier II API Support Engineer

TimG1567
Frequent Participant

Hi David, 

Unfortunately  I do not have the keys I was using prior to the issue. However I will follow up with you if I receive the same error message again. 
 

Thank you once again for all your help. 

View solution in original post

TimG1567
Frequent Participant

Hi David, 

 

I have another question: 

If someone unsubscribes from my email campaigns, and I reupload the contact via API into a list for a particular list_id, does constant contact handle the unsubscribed email list automatically by removing the email_address from the list, or do I need to create logic that would send the unsubscribed contact id into the unsubscribed list? 

Let me know if my question does not make sense. 

Thank you in advance. 

Hello,

 

When a contact is unsubscribed, they must only be added back into your account as active if they take the action themselves. When you are bulk adding contacts, Constant Contact will automatically filter out any previously unsubscribed contacts from your import.

 

Regards,
David Bornstein
Tier II API Support Engineer

Developer Portal

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

Visit Page