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

The*_*dis 5 authentication search azure bing-api odata

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

San*_*tia 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);

...
Run Code Online (Sandbox Code Playgroud)

此代码基于Windows Azure Marketplace文档中迁移到Bing Search API中的PHP示例.

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