Google Shopping API - Ruby

时间:2016-02-24 15:01:13

标签: ruby-on-rails ruby api google-api google-shopping

我在Ruby中与Google Api集成。

我使用的是google-api-client gem,但我在授权请求方面遇到了一些问题。

我的代码:

client_secrets = Google::APIClient::ClientSecrets.load('client_secret_xxxx-.apps.googleusercontent.com.json')

auth_client = client_secrets.to_authorization

auth_client.update!(:scope => 'https://www.googleapis.com/auth/content')  #its possible pass a url to redirect and we can fetch do token

auth_uri = auth_client.authorization_uri.to_s

p auth_uri

此代码返回给google shopping api授权的网址。

在我授予权限后,我将令牌复制到此处:

token = 'token'
merchant_id = 'xxx'
list_products_url = "https://www.googleapis.com/content/v2/#{merchant_id}/products?key=#{token}"
response = HTTParty.get(list_products_url)

这会返回一条错误消息,如下所示:

#<HTTParty::Response:0x7fde5c37b8f0 parsed_response={"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"required", "message"=>"Login Required", "locationType"=>"header", "location"=>"Authorization"}], "code"=>401, "message"=>"Login Required"}}, @response=#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>, @headers={"vary"=>["Origin", "X-Origin"], "www-authenticate"=>["Bearer realm=\"https://accounts.google.com/\""], "content-type"=>["application/json; charset=UTF-8"], "date"=>["Wed, 24 Feb 2016 14:58:17 GMT"], "expires"=>["Wed, 24 Feb 2016 14:58:17 GMT"], "cache-control"=>["private, max-age=0"], "x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "server"=>["GSE"], "alternate-protocol"=>["443:quic,p=1"], "alt-svc"=>["quic=\":443\"; ma=2592000; v=\"30,29,28,27,26,25\""], "connection"=>["close"], "transfer-encoding"=>["chunked"]}>

我不知道我做错了什么..

我试图实现这个简单的例子..

https://developers.google.com/shopping-content/v2/reference/v2/products/list#try-it

最好的问候

0 个答案:

没有答案