简体   繁体   中英

Ruby example to access google shopping api using GAN publisher id

I was wondering if anyone could provide an example of how to pull products from the Google Shopping API using a GAN publisher ID and ruby (google-api-ruby-client). I'm gathering you need to authenticate using oauth. The documentation is very sparse so any help would be much appreciated.

Basic usage of the shopping API with the client is easy.

require 'google/api_client'

client = Google::APIClient.new
client.authorization = nil

shopping = client.discovered_api("shopping", "v1")
result = client.execute(:api_method => shopping.products.list,
                        :parameters => { "source" => "public" })

To query by GAN publisher ID, you need to be authenticated as you're aware. You can use OAuth 2 for that. You can see a sample of that for the ruby client at http://code.google.com/p/google-api-ruby-client/wiki/OAuth2 . The scope to use for shopping is:

 https://www.googleapis.com/auth/shoppingapi

You can use the APIs explorer to try this out pretty quickly:

http://code.google.com/apis/explorer/#_s=shopping&_v=v1&_m=products.list

With version 0.9.11 is even easier

require 'google/apis/content_v2'

def list_products
  content_for_shopping = Google::Apis::ContentV2::ShoppingContentService.new
  content_for_shopping.authorization = get_authorization(%w(https://www.googleapis.com/auth/content))
  content_for_shopping.authorization.fetch_access_token!

  content_for_shopping.list_products(ENV['GOOGLE_MERCHANT_CENTER_ID'])
end

def get_authorization(scopes)
  cert_path = Gem.loaded_specs['google-api-client'].full_gem_path + '/lib/cacerts.pem'
  ENV['SSL_CERT_FILE'] = cert_path
  authorization = Google::Auth.get_application_default(scopes)
  # Clone and set the subject
  auth_client = authorization.dup
  return auth_client
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM