Skip to content

Google Search API for Shopping

This code is obsolete yet still here for historical reasons. Google released an alpha version of the Google API Client gem that will do the trick. It is not a joy to work with, but it works. Also, if you are interested in Google APIs and are looking to use the Custom Search Engine API with ruby, have a look at this.

Google has recently launched a new API for its merchant product data, that can be used publicly by developers, allowing them to search and retrieve product information (like those appearing in Google Product Search and Product Ads). Another interesting fact is that if you happen to be a Google Affiliate Network (GAN) user, you may use this API to retrieve products from affiliated merchants, along with tracked links to those products.

Sounds good, right? Sounds like Google is finally willing to share a teeny-weeny bit of their data with us lowly humans, which is good for Humbucker, as we are currently looking for new and diverse data sources for Product Wave.

Before you consider building production stuff using this API, fair warning: Google have been notoriously flaky when it comes to their product access, Google Base, Products and now this API have been coming and going like crazy while the API itself is still in Labs, which means that changes are bound to happen, and it may even get pulled out completely.

If you are still interested in how this works, here is a code example using ruby and cURL. Before you use it, you will need to create a GAN account, get signed up to a few merchants  with feeds, enable the Product API from the API Console and wait a day for the Product Feeds to kick in. The code below also features a public access API example, which does not return affiliate links but does not require an active GAN account to work.

require "uri"
require "active_support"
 
# Get auth token from Googles
auth_uri = "https://www.google.com/accounts/ClientLogin"
params = { 
  "accountType" => "HOSTED_OR_GOOGLE", 
  "Email" => "yer_gmail", 
  "Passwd" => "yer_gpass", 
  "source" => "organization-appname-version",
  "service" => "shoppingapi"
}
r = `curl -d '#{params.to_param}' '#{auth_uri}'`
auth_token = r.match(/Auth=(.*)/)[1].strip
 
# Run a search query using retrieved token
default_params = {
  :key => "yer_api_key", 
  :country => "US", :alt => "json", :language => "en"
}
 
# To get back products with tracked affiliate links
uri = URI.parse("https://www.googleapis.com/shopping/search/v1/gan:yer_gan_id/products")
# To get back simple - untracked product listings:
# uri = URI.parse("https://www.googleapis.com/shopping/search/v1/public/products")
 
@uri.query = default_params.merge({ :q => "Motorhead" }).to_param
 
cmd = %[curl --header "Authorization: GoogleLogin auth=#{auth_token}" "#{uri}"]
response = JSON.parse(`#{cmd}`)
 
 
puts "Found #{response["totalItems"]} products"
# => Found 5523 products
puts response["items"].collect { |i| i["product"]["title"] }.join("\n")
# => Motorhead - No Remorse Limited MC ... ... ...

Categories: Code.

Tags: , , , , , ,

Comment Feed

4 Responses

  1. Hallo,
    toller Code aber wie bringe ich diesen nun zum laufen?
    Habe alle erforderlichen Voraussetzungen, aber eine kurze Erklärung, wie er aufzurufen ist wäre sehr nett. Übringens ein allgemeiner Hinweis, für alle Laien wie mich doch sehr hilfreich.

    • Hi Jurgen, my German is not very good, but it looks like you re asking how to use this piece of code. This example is intended to be ran as part of a ruby script or application. It will return product data which you then will have to decide on how to process display. as long as you have all the required libraries installed and have a GAN account, you should be able to cut/paste/run this code and get a list of products back. Bear in mind that google doesnt look like it’s very happy with its product API and keeps changing it, so I can’t guarantee this will still work tomorrow.

  2. First, don’t use ClientLogin if you can help it. Second, why not just use the actual Ruby client? So much easier.



Some HTML is OK

or, reply to this post via trackback.