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