Bing中的Bing搜索API Azure Marketplace身份验证

时间:2012-06-21 11:13:56

标签: authentication search azure odata bing-api

如何在Java中进行身份验证以使用Azure Marketplace中的新bing搜索API?迁移指南未向您提供有关Java的信息

1 个答案:

答案 0 :(得分:8)

您需要将您的accountKey编码为Base64,并使用Authorization标头将其传递给每个请求。

String bingUrl = "https://api.datamarket.azure.com/Bing/Search/................";

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

URL url = new URL(bingUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);

...

此代码基于Migrating to the Bing Search API in Windows Azure Marketplace文档中的PHP示例。

更新:修改了encodeBase64调用,它应该是这样的:accountKey +“:”+ accountKey