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)?

sparkle
  • 7,530
  • 22
  • 69
  • 131

3 Answers3

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
Chris Bui
  • 1,055
  • 1
  • 13
  • 24
  • I tried the code above, and it returns `Timeout::Error: execution expired` error. Can you make sure us about latest working version? – mert Dec 12 '12 at 13:17
  • 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. – Chris Bui Dec 12 '12 at 14:53
  • 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.. – Zach Moshe Dec 31 '12 at 09:10
  • thanks for the basic_auth thing. I was trying for an hour to check what's wrong with my Base64 encoder... – Zach Moshe Dec 31 '12 at 09:11
  • This worked perfectly after I installed curb (gem install curb) – Devin McQueeney Dec 10 '13 at 18:19
  • 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. – chovy Jan 31 '17 at 08:04
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.)

jonahb
  • 2,570
  • 16
  • 22
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
opsb
  • 29,325
  • 19
  • 89
  • 99