5

Azure Marketplace から新しい bing 検索 API を使用するために Java で認証するにはどうすればよいですか?移行ガイドでは、Java に関する情報は提供されません。

4

1 に答える 1

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

...

このコードは、Windows Azure Marketplaceドキュメントの Bing Search API への移行にある PHP の例に基づいています。

更新: encodeBase64 呼び出しを変更しました。次のようにする必要があります: accountKey + ":" + accountKey

于 2012-06-21T11:42:28.540 に答える