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
Bing Search API Azure Marketplace Authentication in Java
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
The way seems to be correct but something in the setRequestProperty must be differenet because this way I get the responce message "Basic authentication is required. Enter account key as password. –
Piane
Your new code produces a "Bad Request". I have also tried httpsCon.setRequestProperty("Authorization: Basic",accountKeyEnc); but I got again the message "Basic authentication is required..." @sandrinodimattia –
Piane
Does this code help? github.com/carrot2/carrot2/tree/master/core/… –
Piane
If you get a Bad Request I'm assuming the authentication is OK but there's something wrong with the actual request. What is the complete url you are using (did you handle the url encode for example)? What data are you sending? –
Jaquith
The url I open connection for is "api.datamarket.azure.com/Bing/Search/…". The connection I open is an HttpsURLConnection with name httpsCon. After the line httpsCon.setRequestProperty(....) I just try to see if its connected well through the method getResponseCode which produces code 400 and through getResponceMessage that produces a string "Bad Request". –
Piane
I found the problem of my bad request! I have put $ in front of Query...thanks man for the help! –
Piane
© 2022 - 2024 — McMap. All rights reserved.