cancel
Showing results for 
Search instead for 
Did you mean: 
In recognition of Memorial Day our Support office will be closed Monday, May 28th. We will re-open with normal hours on Tuesday, May 29th.

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

SOLVED
Solution Provider

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. 

Tags (2)
13 REPLIES
Moderator

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

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

Highlighted
Solution Provider

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

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. 

 

Moderator

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

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

Solution Provider

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

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)



Moderator

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

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

Solution Provider

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

Hi David, 


I sent an email to the address below. 

Once again thank you. 

Solution Provider

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

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!

Moderator

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

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

Solution Provider

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

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. 

Still need help?
You can post a new message in the Community or find us on Twitter Mon-Fri 8am - 8pm ET. We've got real people waiting to help you out. Click below to start a conversation!