I'm sending an ajax request to the Bing Search API. The URL I am using for the request works when I put it in the browser. With ajax I get a 401 error "The authorization type you provided is not supported. Only Basic and OAuth are supported"
therefore my header is wrong. It works in the browser because I manually type in my azure account key.
<script>
$scope.bingsearch = function() {
var azurekey = '....vjrQiHPp4ct1meqroX2pzQZhPvE';
var keywords = $scope.inputvalue;
var myurl = 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?
Sources=%27web%27&$format=json&Query=%27'+ keywords + '%27';
$http({
method: 'POST',
url: myurl,
headers:{
'Authorization': 'Basic ' + azurekey
}
}).success(function(data){
var searchresults = angular.fromJson(+data);
$scope.searchresult = searchresults;
})
};
</script>
The URL https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&$format=json&Query=%27van%20gogh%27
works in the browser.
How do I set my header so that it will accept my account key?
I figured it out. I had to Base64 encode my Azure key in the headers, prefixed by a colon. I went to this website and pasted a colon ":" plus my Azure account key and base64'd it.
So my key OQA/cs
becomes :OQA/cs
then base64'd into Ok9RQS9jcw==
and the final header looks like
`headers: {
'Authorization': 'Basic Ok9RQS9jcw=='
}`
and Bing returns the json I want. Do not forget to include the colon as the first character of your account key before you encode it so that you Base64 encode it as part of your key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With