【问题标题】:search api of bing azure marketpalce with java使用 java 搜索 bing azure marketpalce 的 api
【发布时间】:2014-11-04 07:42:15
【问题描述】:

尝试在 java 中使用 bing azure marketpalce 的搜索 api 我有这个代码:

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();
        }
    }

}

我得到一个错误:

线程“main”中的异常 org.apache.http.client.HttpResponseException:授权类型 不支持您提供的。仅支持 Basic 和 OAuth

【问题讨论】:

    标签: java api azure bing


    【解决方案1】:

    我首先看到的是你的台词

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

    应该阅读:

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

    还有你为此使用 apache 库的原因吗?我用于从 bing 获取 json 对象的代码使用 java.net,如下所示:

    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.
      }
    }
    

    要构建 json 对象,我会说遵循以下代码: Convert InputStream to JSONObject

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多