Reputation: 11
I need to invoke a REST call from Java code.
I have credentials in the form of
An algorithm provided which
Gets the server time
Using Id, security key & server time it generates a security token
Now authorization is in the below form
"Authorization": "name id=Id, serverTime=serverTime, securitytoken=securitytoken"
Need a java client program to invoke this REST call using above authorization header.
I am getting
HTTP Response 401 error.
Please provide correct way to set authorization header in request for form
Name Id="Id",serverTime="2017-11-18T05:51:05",securityToken="TOKEN"
Code:
package com.rest.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Sample {
public final static String GATEWAY_ID = "Id";
public final static String KEY = "Key";
public static void main(String[] args) {
try {
HttpURLConnection conn;
conn = (HttpURLConnection) new URL("https://domain/A/B/72968").openConnection();
String serverTime = "2017-11-18T10:51:05";
String securityToken = "TOKEN";
String authorization = "Name Id=\"" + GATEWAY_ID + "\",serverTime=\"" + serverTime + "\",securityToken=\""
+ securityToken + "\"";
// Name Id="Id",serverTime="2017-11-18T10:51:05",securityToken="TOKEN"
conn.addRequestProperty("Authorization", authorization); // Is Header set is correct? It should be part of Request Header. Please correct this
int status = conn.getResponseCode();
System.out.println(status);
BufferedReader br = null;
StringBuilder body = null;
String line = "";
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
body = new StringBuilder();
while ((line = br.readLine()) != null) {
body.append(line);
}
System.out.println(body);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
Error:
java.io.IOException: Server returned HTTP response code: 401 for URL: https://domain/A/B/72968 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at com.rest.client.ApplicationClient.main(ApplicationClient.java:48) Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://domain/A/B/72968 at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) at com.rest.client.ApplicationClient.main(ApplicationClient.java:38)
Upvotes: 0
Views: 2268
Reputation: 2560
try format authorization string this way "Basic Base64"
. try
authorization ="Basic "+new String(new Base64().encode(authorization.getBytes()));
conn.setRequestProperty("Authorization", authorization);
Upvotes: 1