Hello,
I'm trying to update our application to use Basic authentication over https.
Two changes have been made:
1. URI looks like this now: "https://api.constantcontact.com/ws/customers/ourcompany/activities"
2. HttpClient API call has been change to use 'AuthPolicy.BASIC'.
Also, according to this resource: http://hc.apache.org/httpclient-3.x/sslguide.html, with HttpClient, we don't need to do anything extra in order to use SSL communications.
My problem is that, I'm getting 401 error with the following message:
Jun 20, 2009 12:12:10 AM org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
INFO: No credentials available for BASIC 'api.constantcontact.com'@api.constantcontact.com:443
Could, please, anybody help me to resolve this issue.
Thanks in advance,
Mike
This response indicates that the Basic Authentication is not being set corretly in your HttpClient class. I would refer to the documentation on that class on how to implement Basic Authentication over a secury connection (https). Have you tried running the test case they suggested for ensuring your Apache server is set up correctly?
No, I have not ran that test.
Could you please point me where I can find it?
Dave,
I can't find any other settings for HttpClient in order to set it for basic authentication.
Would it be helpfull if I paste my code for your review?
Dave, I'm not getting anywhere and time is running out.
I'm posting trimmed down version of my code. Could you please take a look and see what is wrong.
public class ConstantContactActivityUpload {
public void upload(List<ConstantContactUploadInfo> infoList, int listKey) throws IOException {
UsernamePasswordCredentials credentials = setCredentials();
HttpClient httpClient = initialSetUp(credentials);
AuthDetails authDetails = extractAuthDetails();
RequestEntity requestEntity ...
String url = "https://api.constantcontact.com/ws/customers/ourcompany/activities";
this.postToServer(httpClient, requestEntity, url, uploadedValues.toString());
}
private HttpClient initialSetUp(UsernamePasswordCredentials credentials) {
HostConfiguration hostConfiguration = new HostConfiguration();
hostConfiguration.setHost("api.constantcontact.com");
AuthScope authScope = new AuthScope("api.constantcontact.com", 80, "api.constantcontact.com");
AuthPolicy.registerAuthScheme("Basic", BasicScheme.class);
HttpClient httpClient = new HttpClient();
List<String> list = new ArrayList<String>();
list.add(AuthPolicy.BASIC);
httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, list);
httpClient.getState().setCredentials(authScope, credentials);
return httpClient;
}
private void postToServer(HttpClient client, RequestEntity requestEntity, String url, String uploadedValues) throws HttpException, IOException {
PostMethod httpPost = new PostMethod(url);
httpPost.setRequestEntity(requestEntity);
client.executeMethod(httpPost);
}
private UsernamePasswordCredentials setCredentials() {...}
protected AuthDetails extractAuthDetails() {...}
}
Change the port fom 80 to 443 or to a negative number or to AuthScope.ANY_PORT.