Account Provisioning In Java

New Member

Account Provisioning In Java

 Maybe I'm just not understanding the API correctly, but I keep getting a 401 error when trying to add a new trial account.  Where do I actually use the API key?

//partnername being the login name we were provided

URL url = new URL("");

StringBuilder xml = new StringBuilder();


// build the xml

xml.append("<atom:entry xmlns:atom=\"\">");


xml.append("<atom:title />");

xml.append("<atom:author />");

xml.append(String.format("<atom:updated>%s</atom:updated>", new Date()));

xml.append("<atom:content type=\"application/vnd.ctct+xml\">");

xml.append(" <SiteOwner>");

xml.append(String.format("<LoginName>%s</LoginName>", customer.getLogInName() != null ? customer.getLogInName() : ""));

xml.append(String.format("<Password>%s</Password>", customer.getPassword() != null ? customer.getPassword() : ""));

xml.append("  <Site>");

xml.append(String.format(" <Name>%s</Name>", customer.getSite_Name() != null ? customer.getSite_Name() : ""));

xml.append(String.format(" <Phone>%s</Phone>", customer.getSite_Phone() != null ? customer.getSite_Phone() : ""));

xml.append(String.format(" <URL>%s</URL>", customer.getSite_Url() != null ? customer.getSite_Url() : ""));

xml.append(String.format(" <SignatureName>%s</SignatureName>", customer.getSite_SignatureName() != null ? customer.getSite_SignatureName() : ""));

xml.append("  </Site>");

xml.append(String.format("<EmailCode>%s</EmailCode>", customer.getEmailCode() != null ? customer.getEmailCode() : ""));

xml.append("  <SiteContact>");

xml.append(String.format(" <FirstName>%s</FirstName>", customer.getSiteContact_FirstName() != null ? customer.getSiteContact_FirstName() : ""));

xml.append(String.format(" <LastName>%s</LastName>", customer.getSiteContact_LastName() != null ? customer.getSiteContact_LastName() : ""));

xml.append(String.format(" <Email>%s</Email>", customer.getSiteContact_Email() != null ? customer.getSiteContact_Email() : ""));

xml.append(String.format(" <Phone>%s</Phone>", customer.getSiteContact_Phone() != null ? customer.getSiteContact_Phone() : ""));

xml.append(String.format(" <CountryCode>%s</CountryCode>", customer.getSiteContact_CountryCode() != null ? customer.getSiteContact_CountryCode()

: ""));

xml.append(String.format(" <StateCode>%s</StateCode>", customer.getSiteContact_StateCode() != null ? customer.getSiteContact_StateCode() : ""));

xml.append("  </SiteContact>");

xml.append("  <Products>");

if (customer.getProducts() != null)


if (customer.getProducts() != null)

xml.append(String.format(" <Product>%s</Product>", customer.getProducts()));

if (customer.getProducts().length >= 2 && customer.getProducts() != null)

xml.append(String.format(" <Product>%s</Product>", customer.getProducts()));

if (customer.getProducts().length >= 3 && customer.getProducts() != null)

xml.append(String.format(" <Product>%s</Product>", customer.getProducts()));


xml.append("  </Products>");

xml.append(String.format("<AccountId>%s</AccountId>", customer.getAccountId() != null ? customer.getAccountId() : ""));

xml.append(String.format("<SingleBilling>%s</SingleBilling>", customer.isSingleBilling()));

xml.append(String.format("<ReferralCode>%s</ReferralCode>", customer.getReferralCode() != null ? customer.getReferralCode() : ""));

xml.append(" </SiteOwner>");




URLConnection urlC = url.openConnection();



urlC.setRequestProperty("accept", "application/atom+xml");


PrintStream stream = new PrintStream(urlC.getOutputStream());



// check the result

// crashes out on next line

BufferedReader br = new BufferedReader(new InputStreamReader(urlC.getInputStream()));

String result = null;

while ((result = br.readLine()) != null)


// going to log the result of:





} catch (MalformedURLException e)


logger.error("Could not create an account for " + customer, e);



The URLConnection class is an

The URLConnection class is an older class that does support authentication, however it is not the most obvious way to do so.  You must set the RequestParams for Username and Password to make sure the request authenticates correctly.  Here is a tutorial:

Most Java Developers use the Apache HttpClient class to do there HTTP WebRequest development.  This Class supports many more advanced features and makes sending WebRequests much easier.  It includes an authentication class that you can set up as well as additional options that greatly increase ease of development.  You can find this library here:

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More