2

I am using this code which utilizes an odata4j ODataClientRequest and ODataConsumer to attempt to call an OData service that requires authentication:

    String url = "https://mylocalhost/api/odata/People()?$filter=PID%20eq%20'10'";

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("AccountID", "100");
    ODataClientRequest clientRequest = new ODataClientRequest("GET", url, headers, null, null);

    ODataConsumer consumer = ODataConsumer.create(url);

    for(OEntity entity : consumer.getEntities("People").execute()){

However, I'm getting an authentication error, because the server is requesting header authentication information. How can I create my ODataConsumer that includes the required authorization header information?

atconway
  • 20,624
  • 30
  • 159
  • 229

1 Answers1

-2

Instead of manually adding a header, I believe you can use Basic Authentication (since you received an authentication error) on the client and can add a built-in client "behavior" when you set up your consumer. The code for BasicAuthenticationBehavior.java is displayed in the follwing link:

BasicAuthenticationBehavior.java

The code for adding the basic authentication behavior to your ODataConsumer would look similar to the following:

ODataConsumer.Builder builder = ODataConsumers.newBuilder(url);
builder.setClientBehaviors(new BasicAuthenticationBehavior(LoginUsername, LoginPassword));      
ODataConsumer c = builder.build();

for(OEntity entity : c.getEntities("EntityName").execute()){
    System.out.println(entity.getProperty("Name").getValue().toString());
}
atconway
  • 20,624
  • 30
  • 159
  • 229