使用Google Content API进行购物验证时出现问题



我正在尝试将OAuth2用于服务器到服务器应用程序,并使用Google的Content API用于购物,使用Google-API-client gem和Ruby on Rails 3.2.5。此外,我已经按照内容API文档中的规定设置了我的商家帐户。

这是我发现的最好的方法:

  • 在后台创建/更新产品
  • 创建了属于我公司谷歌产品"保护伞"的产品
  • 不要求每个用户在其令牌过期时进行身份验证/授权

使用这个示例中的第1-23行作为起点,我已经开始编写以下用于后台作业的模块:

require 'httparty'
require 'google/api_client'
module GoogleProducts
  GOOGLE_CONFIG = YAML.load_file(File.join(Rails.root, "config", "google.yml"))[Rails.env]
  CLIENT_ID = "XXXXXXXXXXXX@developer.gserviceaccount.com"
  MERCHANT_ID = "XXXXXXX"
  SCOPE = "https://www.googleapis.com/auth/structuredcontent"
  KEY_FILE_PATH = File.join(Rails.root, "config", "my-privatekey.p12")
  KEY_FILE_PASS = "XXXXXXXXXX"
  def self.add_item(item_id)
    self.fetch_token
    xml = self.gen_item_xml(item_id)
    headers = {"Content-type" => "application/atom+xml", "Content-Length" => xml.length.to_s}
    url = "https://content.googleapis.com/content/v1/#{MERCHANT_ID}/items/products/generic?access_token=#{$gp_token}"
    response = HTTParty.post(url, :body => xml, :headers => headers).parsed_response
  end
  def self.gen_item_xml(item_id)
    #building product xml
  end
  private
  def self.fetch_token
    api_client = Google::APIClient.new(:authorization => :oauth2)
    key = Google::APIClient::PKCS12.load_key(KEY_FILE_PATH, KEY_FILE_PASS)
    asserter = Google::APIClient::JWTAsserter.new(CLIENT_ID, SCOPE, key)
    begin
      api_client.authorization = asserter.authorize
      #todo - store in something other than a global
      $gp_token = api_client.authorization.access_token
    rescue Signet::AuthorizationError => e
      puts e.message
    ensure
      return $gp_token
    end
  end
end

一切似乎都很好——身份验证、身份验证令牌的处理——直到我尝试实际添加一个项目,当我这样做时,我会得到以下结果:

<errors xmlns='http://schemas.google.com/g/2005'>
  <error>
    <domain>GData</domain>
    <code>ServiceForbiddenException</code>
    <internalReason>Could not find authenticated customer</internalReason>
  </error>
</errors>

有什么想法吗?

经过许多痛苦和脑力劳动,我终于解决了我的问题!

由于我使用OAuth 2服务器到服务器身份验证,hjblok给出的建议不适用(不过,感谢您尝试!)。

我只是简单地将与我的服务帐户密钥相关联的电子邮件地址从Google API控制台(例如XXXXXXXXXXXX@developer.gserviceaccount.com)添加到我的Google Merchant帐户(商家管理页面上的Settings > Users),它就起到了作用。

如果需要任何澄清,请随时发表评论!

Google Content API文档说明您需要在Google Merchant Center的设置页面中进行设置:

https://developers.google.com/shopping-content/getting-started/usingapi-products

编辑在深入谷歌API文档后重写答案

你已经尝试过使用谷歌的OAuth 2.0游戏了吗?我能够成功地访问https://content.googleapis.com/content/v1/#{MERCHANT_ID}/items/products/generic

  1. 在"步骤1"中,我选择了"购物内容API",然后使用我的帐户授权API
  2. 然后在"步骤2"中,我"用授权码交换了令牌",这将产生一个"刷新令牌"和一个"访问令牌"
  3. 然后在"步骤3"中,我调用了一个GEThttps://content.googleapis.com/content/v1/1234567/items/products/generic的请求。因为1234567不是有效的MERCHANT_ID,所以它返回一个Error。但是错误消息包含一个MERCHANT_ID,它实际上属于您的帐户
  4. 我重复了"步骤3",但现在使用了正确的MERCHANT_ID。它返回一个HTTP/1.1 200 OK,其中包含正文中的请求项

此外,我不确定,但Google API不希望access_token$gp_token)出现授权头吗?在OAuth 2.0平台中,此Authorization头用于发送access_token

我还找到了结构化内容API演示页面(https://google-content-api-tools.appspot.com/demo/demo.html),更具体地说是用于购物的内容API。

最新更新