Bing Search API Authentication
This may seem a bit obvious to some, but this drove me nuts for a bit while trying to code up a Bing search service. When submitting the request using a GUI client and curl, the 'basic' in the basic auth header was not case sensitive. However, when submitting through code (Java and Groovy) the 'basic' had to be capitalized ('Basic') or I would get an error stating that the service only accepts Basic and OAuth authentication requests. As if this wasn't enough to drive you crazy there's another caveat. When submitting using curl, a gui, or through the web page you have to specify your api key twice (user/pw). However, when submitting through code, you only specify the key once in the password field. However, you do have to encode a ':' in front of your password or the service won't work. So, in summary, here are some examples:
// Works
def authString = ":${apikey}".getBytes().encodeBase64()
// Header should look like this
["Authorization": ["Basic ${authString}"]]
// Does not work. No leading colon (:) in the auth string. 'Basic' is lowercase
def authString = "${apikey}".getBytes().encodeBase64()
// Header should look like this
["Authorization": ["basic ${authString}"]]
I hope you'll find this as useful as I would have earlier today. :)
Written by Michael Johnson
Related protips
1 Response
Seems that every API out there has its own quirky way to authenticate. Using Basic authentication with the API key as the password and leaving the username blank is a new one to me.
Case sensitive strings in Java but not on the MS receiving side doesn't surprise me as much. I've seen incorrect case issues in Java with the HTTP header names which result in duplicate header names with different cases.