2

我正在使用此代码,它利用 odata4jODataClientRequestODataConsumer尝试调用需要身份验证的 OData 服务:

    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()){

但是,我收到了身份验证错误,因为服务器正在请求标头身份验证信息。如何创建ODataConsumer包含所需授权标头信息的我的?

4

1 回答 1

-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());
}
于 2013-05-14T15:01:30.890 回答