Bing Search API Azure Marketplace Authentication in Java

给你一囗甜甜゛ 提交于 2019-11-27 07:00:08

问题


How can I authenticate in Java to use the new bing search api from Azure Marketplace?The migration guide does not provide you with info about Java


回答1:


You'll need to encode your accountKey to Base64 and pass it to each request using the 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);

...

This code is based on the the PHP example found in the Migrating to the Bing Search API in Windows Azure Marketplace document.

Update: Modified the encodeBase64 call, it should be like this: accountKey + ":" + accountKey



来源:https://stackoverflow.com/questions/11136936/bing-search-api-azure-marketplace-authentication-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!