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

最佳答案

您需要将accountKey编码为Base64,然后使用Authorization header 将其传递给每个请求。

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

10-08 04:58