[英]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). 我想知道是否有人可以提供一个如何使用GAN发布者ID和ruby(google-api-ruby-client)从Google Shopping API中提取产品的示例。 I'm gathering you need to authenticate using oauth.
我正在收集您需要使用oauth进行身份验证。 The documentation is very sparse so any help would be much appreciated.
文档很稀疏,所以任何帮助将非常感激。
Basic usage of the shopping API with the client is easy. 购物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" })
To query by GAN publisher ID, you need to be authenticated as you're aware. 要按GAN发布商ID进行查询,您需要根据需要进行身份验证。 You can use OAuth 2 for that.
您可以使用OAuth 2。 You can see a sample of that for the ruby client at http://code.google.com/p/google-api-ruby-client/wiki/OAuth2 .
您可以在http://code.google.com/p/google-api-ruby-client/wiki/OAuth2上查看ruby客户端的示例。 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: 您可以使用API资源管理器快速尝试:
http://code.google.com/apis/explorer/#_s=shopping&_v=v1&_m=products.list
With version 0.9.11 is even easier 使用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.