Azure Bing Web search fails with Query search

2020-04-10 00:12发布

问题:

I am using the Odata4j. When i try to execute for simple Web search with my query, getting following exeception at last line of the code:

PS: Please voteup if you find this helpful.Thanks

java.lang.RuntimeException: Expected status OK, found Bad Request. Server response:
Parameter: Query is not of type String
    at org.odata4j.jersey.consumer.ODataJerseyClient.doRequest(ODataJerseyClient.java:165) 

This is my code:

ODataConsumer consumer = ODataConsumers
                .newBuilder("https://api.datamarket.azure.com/Bing/Search/v1/")
                .setClientBehaviors(OClientBehaviors.basicAuth("accountKey", "My account key here"))
                .build();


  System.out.println(consumer.getServiceRootUri()+consumer.toString());

  OQueryRequest<OEntity> oQueryRequest = consumer.getEntities("Web").custom("Query", "Search text criteria");


    System.out.println("oRequest"+oQueryRequest);

        Enumerable<OEntity> entities  = oQueryRequest.execute();

回答1:

Not much into Java, but I've just got the same error with the implementation for Node.js, and the problem was that I forgot to add single quotes around the query. It should be something like:

...&Query='stackoverflow'

so after the URL encoding we have something like:

...&Query=%27stackoverflow%27

At least that worked for me.



标签: azure bing