简体   繁体   中英

search api of bing azure marketpalce with java

Tring to use search api of bing azure marketpalce with java I have this code :

import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class BingAPI2 {
    public static void main(String[] args) throws Exception{
        BingAPI2 b = null;
        b.getBing();

    }

public static void getBing() throws Exception {

        HttpClient httpclient = new DefaultHttpClient();

        try {
            String accountKey = "myAccountKey=";
            byte[] accountKeyBytes = Base64.encodeBase64((":" + accountKey).getBytes());
            String accountKeyEnc = new String(accountKeyBytes);

            HttpGet httpget = new HttpGet("https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?$Query=%27Datamarket%27&$format=json");
            httpget.setHeader("Authorization", "Basic <"+accountKeyEnc+">");

            System.out.println("executing request " + httpget.getURI());

            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
            System.out.println("----------------------------------------");

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }

}

I get an error :

Exception in thread "main" org.apache.http.client.HttpResponseException: The authorization type you provided is not supported. Only Basic and OAuth are supported

first thing I see is that your line

byte[] accountKeyBytes = Base64.encodeBase64((":" + accountKey).getBytes());

should read :

byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());

also is there a reason you're using the apache libraries for this? the code I use for getting json objects from bing uses java.net and looks like this:

import java.net.URLConnection;
import java.net.URL;
import java.io.InputStreamReader;

class BingJson{

  JSONObject getJSONfromBing(String term){
  try{
    URLConnection c = new URL(term).openConnection();
    String key = (DatatypeConverter.printBase64Binary(("XXX" + ":" + "XXX").getBytes("UTF-8")));
    c.setRequestProperty("Authorization", String.format("Basic %s",key));
    c.connect();
    //etc.
  }
}

to build the json object I would say follow this code: Convert InputStream to JSONObject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM