Bing Search API in Ruby
Asked Answered
J

3

5

I read the "Bing Search API - Quick Start" but I don't know how to make this http request in Ruby (Weary)

How to translate "Stream_context_create()" in Ruby? And What does it mean?

"Bing Search API - Quick Start" enter image description here

I would want to use a Ruby sdk but those I found are deprecated ex (Rbing) https://github.com/mikedemers/rbing Do you know a up-to-date Wrapper for Bing Search API (Web only results)?

Justify answered 1/12, 2012 at 14:38 Comment(4)
You could just construct a search URL and and scrape the resulting HTML. Dirty, but it works.Tommietommy
Have you looked at what comes up on ruby-toolbox? ruby-toolbox.com/search?utf8=%E2%9C%93&q=bing+searchYatzeck
Yes, they all refer to deprecated version of API. (They are older 9months ago). If I would write http request on my own, how I could do?Justify
I'm trying to figure this out right now.Protoactinium
P
7

Okay, after an hour of frustration I figured out a way to do it. This code is awful because it's the first version I got working. Basically, ignore everything about the base64 encode because it was giving me an error that only oAuth and basic authentication was supported. Turns out Microsoft's documentation was wrong and you're supposed to just use your account key as the password in the request instead of the encoded string.

require 'net/http'

accountKey = 'KEY'

url = 'https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$top=50&$format=json'

uri = URI(url)

req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth '', accountKey

res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
  http.request(req)
}
puts res.body
Protoactinium answered 2/12, 2012 at 2:37 Comment(6)
I tried the code above, and it returns Timeout::Error: execution expired error. Can you make sure us about latest working version?Radiology
I still use the same code to make the request. I don't think the Timeout::Error: execution expired error is related to the request.Protoactinium
If you get timeout error, it's probably because you're trying to connect with http and not https (that's what happened to me). Make sure your URL is correct, and make sure you've added use_ssl: true to Net::HTTP.start. Hope it helps..Misfit
thanks for the basic_auth thing. I was trying for an hour to check what's wrong with my Base64 encoder...Misfit
This worked perfectly after I installed curb (gem install curb)Overactive
I'm getting a lot of ESOCKETTIMEDOUT errors in node.js with Bing API v2. Is anyone else having this issue? The other apis I call (twitter, youtube) all work fine.Arise
S
2

Try the bing-search gem:

require 'bing-search'

BingSearch.account_key = <your key>
BingSearch.web_only = true
results = BingSearch.web('stack overflow')

Documentation is here and source is on GitHub. (Disclaimer: I wrote the bing-search gem.)

Steeplejack answered 22/1, 2015 at 4:6 Comment(0)
T
1

Wow, microsoft docs eh, something so simple and I've spent 30 minutes trawling the net to find out how to use it. Anyway, here's another take on Chris Bui's answer, using RestClient:

class BingSearch
    def self.for(account_key, query)
        puts RestClient.get("https://:#{account_key}@api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query='#{CGI::escape(query)}'&$format=json")
    end
end
Tartan answered 19/9, 2013 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.