09-03-2014 04:47 PM - edited 03-14-2019 01:50 PM
We have successfully authenticated but the only way we were able to figure out how was to manually encode (see below):
java.net.URLConnection c = new java.net.URL("http://192.168.1.111/adminapi/campaign").openConnection();
c.setDoInput(true);
c.setRequestProperty("Authorization","Basic dWNjeGFkbWluOmlwY3NjMTI=");
c.connect();
Does anyone know how to complete this authentication without manually encoding? Any help/input is appreciated! Thanks!
09-04-2014 01:18 AM
Hi
Try this:
c.setRequestProperty("Authorization","Basic " + com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(("username" + ":" + "password").getBytes()).trim());
Swapping out "username" and "password" for better strings or variables.
I should mention that in my IDE this 'xerces' class is flagged as 'propriety, internal and may be removed from Java in a future release'... however it's still there in JDK 8 so I wouldn't sweat it too much. The other option is you'd have to load up another API or google an example of manual encoding (there are some) but this is a good one-liner.
Regards
Aaron
Please rate helpful posts..
09-04-2014 02:18 AM
Hi,
how 'bout this:
new String(javax.xml.bind.DatatypeConverter.printBase64Binary(orig.getBytes()));
In action:
Both orig and encoded are Strings.
Tested with UCCX 8.0 (JDK 6).
G.
P.S.: Aaron. com.sun.*?! Not cool.
09-04-2014 10:52 AM
Thank you both for your quick reply!! We tested both solutions and they both worked!
09-05-2014 06:53 AM
Hi G
Yeah, I kind of knew that... but never got round to looking up a better way :-)
+5
Aaron
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide