未经授权的bing搜索API

时间:2013-11-07 19:24:34

标签: java azure

以下代码:

HttpClient client = new DefaultHttpClient();
try{
HttpGet request = new HttpGet(x);
request.setHeader("Authorization", "Basic:accound id");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(request, responseHandler);
System.out.println(responseBody);
}

其中x = =“https://api.datamarket.azure.com/Bing/SearchWeb/vl/Web?Query=%27query%20”;

给出以下错误: org.apache.http.client.HttpResponseException:不支持您提供的授权类型。仅支持Basic和OAuth 什么可以解决它?

2 个答案:

答案 0 :(得分:1)

授权标头需要是Base64编码的;用户名为空,密码为帐户ID。

答案 1 :(得分:0)

代码应该是


String accountKey = "xxxxxxxxxxx";
byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());      String accountKeyEnc = new String(accountKeyBytes);
request.setHeader("Authorization",
                "Basic" + " "+accountKeyEnc);