Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bing Search API Azure Marketplace Authentication in Java

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

like image 767
Themis Mavridis Avatar asked Jun 21 '12 11:06

Themis Mavridis


1 Answers

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

like image 140
Sandrino Di Mattia Avatar answered Nov 12 '22 23:11

Sandrino Di Mattia