0

I'm trying to connect to Azure data market, it's an odata repository. I'm using the latest Olingo library, r4.2.0. The following code :

String serviceUrl = "https://api.datamarket.azure.com/DataGovUK/MetOfficeWeatherOpenData/v1/";

ODataClient client = ODataClientFactory.getClient();
ODataServiceDocumentRequest req = client.getRetrieveRequestFactory().getServiceDocumentRequest(serviceUrl);
req.setAccept("application/json;application/xml;odata.metadata=full");
req.setContentType("application/json;application/xml;odata.metadata=full");
ODataRetrieveResponse res = req.execute();

returns an exception

 org.apache.olingo.client.api.communication.ODataClientErrorException: null [HTTP/1.1 415 Unsupported Media Type]

The server returns :

<?xml version="1.0" encoding="utf-8"?>
<m:error mlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
   <m:code />
   <m:message xml:lang="en-US">
       Unsupported media type requested.
   </m:message>
</m:error>

Somebody tried to connect to Azure data market with this library or another with Java?

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
ic3
  • 7,917
  • 14
  • 67
  • 115

1 Answers1

2

Try to set the request headers Accept & Content-Type with only JSON or XML, not both. Please see below.

req.setAccept("application/json");
req.setContentType("application/json;odata.metadata=full");

Or

req.setAccept("application/atom+xml,application/xml");
req.setContentType("application/atom+xml,application/xml;odata.metadata=full");

and for the authorization

req.addCustomHeader("Authorization", "Basic " + getAccountKey());

where account key as described in this other stackoverflow post is:

public String getAccountKey()
{
    String accountKey = "My Microsoft Azure Account Key";
    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    return accountKeyEnc;
}
Community
  • 1
  • 1
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Better, thanks. I'm getting now an 'HTTP/1.1 401 Unauthorized'. Any idea how to login with the 'Primary Account Key' ? – ic3 Jun 27 '16 at 08:32
  • @ic3 You can refer to the answer for the SO thread http://stackoverflow.com/questions/11136936/bing-search-api-azure-marketplace-authentication-in-java. – Peter Pan Jun 27 '16 at 08:42