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("https://api.constantcontact.com/ws/partners/partnername/siteowners");
StringBuilder xml = new StringBuilder();
// build the xml
xml.append("<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\">");
xml.append("<atom:id>data:,</atom:id>");
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>");
xml.append("</atom:content>");
xml.append("</atom:entry>");
URLConnection urlC = url.openConnection();
urlC.setDoOutput(true);
urlC.setAllowUserInteraction(false);
urlC.setRequestProperty("accept", "application/atom+xml");
PrintStream stream = new PrintStream(urlC.getOutputStream());
stream.print(xml.toString());
stream.close();
// 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:
getResultFromCode(Integer.valueOf(result));
}
br.close();
} catch (MalformedURLException e)
{
logger.error("Could not create an account for " + customer, e);
}
... View more