Skip to content

Instantly share code, notes, and snippets.

@Achillefs
Created April 13, 2011 16:03
Show Gist options
  • Save Achillefs/917826 to your computer and use it in GitHub Desktop.
Save Achillefs/917826 to your computer and use it in GitHub Desktop.
Perform product searches using the new Google Products API
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 ... ... ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment