How to do Bing search with HTTP.call


#1

I’ve struggled for a full day to get this working and tried all combinations of “auth”, “Authorization”, appkey once and twice, with “:” between, with and without base64 encoding - but always I am getting this error code:

The authorization type you provided is not supported. Only Basic and OAuth are supported

My code looks as follows:

var authHeader = "Basic " + Base64.encode(appKey + ":" + appKey);

var options = {
   headers: {'Content-Type': 'application/json'},
   'Authorization': authHeader,
   query:"something"
};
HTTP.call('GET', 'https://api.datamarket.azure.com/Bing/Search/v1/Web/', options, function(error, result) {
   if (error) {
      console.log('ERRR');
      console.log(error);
   } else {
      console.log('RESULT');
      console.log(result);
   }
});

Any help will be much appreciated!!

Any ideas for something else I can try will be much appreciated!!

There should be a Meteor package for doing this…


#2

Looking at the Azure docs it may be something like this (I tried to get an API key to test, but there was way too much red tape to go through):

  var authHeader = "Basic " + Base64.encode(appKey + ":" + appKey);

  var options = {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': authHeader
    },
    query: 'something'
  };
  HTTP.call('GET', 'https://api.datamarket.azure.com/Bing/Search/v1/Web/', options, function(error, result) {
    if (error) {
      console.log('ERRR');
      console.log(error);
    } else {
      console.log('RESULT');
      console.log(result);
    }
  });

#3

Ok, I bit the bullet, powered through the red tape and got this working. My code is as follows (note the lack of “v1” in the URL and the addition of the JSON request format). Also, you need to wrap the search query in single quotes - hence the rather ugly coding (you may like to improve that :smile:):

  var authHeader = "Basic " + Base64.encode(appKey + ":" + appKey);

  var options = {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': authHeader
    },
    params: {
      Query: "'something'",
      '$format': 'json'
    }
  };
  HTTP.call('GET', 'https://api.datamarket.azure.com/Bing/Search/Web', options, function(error, result) {
    if (error) {
      console.log('ERRR');
      console.log(error);
    } else {
      console.log('RESULT');
      console.log(result);
    }
  });

Edit: if you include “v1” and remove the JSON requirement you get XML back, which is a real pain to work with!


#4

Many many many thanks, that clearly fixed the problem - also for me!!

You’ve saved me another countless number of hours. Hopefully many others will also find the response useful.

I do - however - get another problem now. The HTTP.call still fails with an error code, this time:

Error: failed [404]
at makeErrorByStatus (http://localhost:3000/packages/http.js?c3c7d0da66f683af57294173733637845f39253a:47:10)
at XMLHttpRequest.HTTP.call.xhr.onreadystatechange (http://localhost:3000/packages/http.js?c3c7d0da66f683af57294173733637845f39253a:301:21)

I’ve got the http package installed - I just checked again.

Any ideas?


#5

I should add also that in the console I also see:

https://api.datamarket.azure.com/Bing/Search/Web/?Query='something'&%24format=json 404 (Not Found)


#6

I changed the link to https://api.datamarket.azure.com/SearchWeb and I now actually don’t get an error anymore.
I do get results - but an empty set of results. Which is also very strange…


#7

Finally, I cracked it. The correct link to use is:

https://api.datamarket.azure.com/Bing/Search/v1/Web


#8

Well, neither of those links are quite correct. Note in my code there is no / at the end of the URL:

https://api.datamarket.azure.com/Bing/Search/Web

LOL: you beat me to it. Although it turns out the “v1” may be left in and JSON will still be returned if requested.


#9

I now want to extend the above query with the option “EnableHighlighting” which is supported by Bing.

My immediate idea was to extend options as such:

var options = {
    headers: {
        'Content-Type': 'application/json',
        'Authorization': authHeader
    },
    params: {
        'Query': query,
        '$format': 'json',
        'Options': 'EnableHighlighting'
    }
};

But it doesn’t work. It actually results in a server error:

M…r.m…e.errorClass {error: 500, reason: "Internal server error", details: undefined, message: "Internal server error [500]", errorType: "Meteor.Error"}

Any ideas?


#10

I got it working. It should be:

‘Options’: “‘EnableHighlighting’”

So double quotes…