簡體   English   中英

使用GAN發布者ID訪問google shopping api的Ruby示例

[英]Ruby example to access google shopping api using GAN publisher id

我想知道是否有人可以提供一個如何使用GAN發布者ID和ruby(google-api-ruby-client)從Google Shopping API中提取產品的示例。 我正在收集您需要使用oauth進行身份驗證。 文檔很稀疏,所以任何幫助將非常感激。

購物API與客戶端的基本用法很簡單。

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" })

要按GAN發布商ID進行查詢,您需要根據需要進行身份驗證。 您可以使用OAuth 2。 您可以在http://code.google.com/p/google-api-ruby-client/wiki/OAuth2上查看ruby客戶端的示例。 用於購物的范圍是:

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

您可以使用API​​資源管理器快速嘗試:

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

使用0.9.11版本更容易

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM