Has anyone successfully implemented this CAM authentication (with and without Integrated login)? Could you please share some sample code in java? I always get 401 Unauthorized response code.
Here is my sample code:
Code: Select all
public class CAM_Try {
public static void main(String[] args) {
System.out.println("testing starts");
//for localhost testing only
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname
, javax.net.ssl.SSLSession sslSession) {
if (hostname.equals("localhost")) {
return true;
}
return false;
}
}
);
try {
URL u = new URL("http://169.46.63.237:50102/api/v1/$metadata");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
String encoded = Base64.encodeBase64String
("<userID>:<password>:<CAMNamespace ID>".getBytes());
c.setRequestProperty("Authorization", "CAMNamespace " + encoded);
System.out.println("\n encoded" + encoded);
c.setRequestProperty("Accept","application/json; charset=utf-8");
c.setRequestProperty("Content-Type","application/json;charset=utf-8");
c.setRequestProperty("WWW-Authenticate"
,"CAMPassport http://ibmdemo.demos.ibm.com:9300/bi/v1/disp, CAMNamespace");
c.setRequestMethod("GET");
c.connect();
int status = c.getResponseCode();
System.out.println(status);
System.out.println( "Response1 "+ c.getResponseMessage()+ "\n\n");
Map<String, List<String>> map = c.getHeaderFields();
System.out.println("Printing Response Header...\n");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
}
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(
new InputStreamReader(c.getInputStream())
);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
} catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
System.err.println(Arrays.toString(ex.getStackTrace()));
} catch (IOException ex) {
System.err.println(ex.getMessage());
System.err.println(Arrays.toString(ex.getStackTrace()));
ex.printStackTrace();
}
}
}
*************************************************************************************
D:\TestApi_Try>java SSLTester
testing starts
encodedYWRtaW5pc3RyYXRvcjpJQk1EZW0wczpIYXJtb255IEFE
401
Response1 Unauthorized
Printing Response Header...
Key : null ,Value : [HTTP/1.1 401 Unauthorized]
Key : WWW-Authenticate ,Value : [CAMPassport http://ibmdemo.demos.ibm.com:9300/bi/v1/disp, CAMNamespace]
Key : Content-Length ,Value : [0]
Key : Set-Cookie ,Value : [TM1SessionId=RjWJ-6VaVUbOEVTl9IZQKQ; Path=/api/; Http
Only]
Key : Connection ,Value : [keep-alive]
Key : Content-Type ,Value : [text/plain]
*************************************************************************************
What am I missing? Please help. Thanks.