访问Google API for Shopping时,凭据无效

时间:2012-12-07 17:25:03

标签: ruby google-api google-api-client google-shopping-api

我正在尝试使用google-api-client gem来访问Google Search API for Shopping。不幸的是,尽管通过Google APIs Console创建了一个access_token,我似乎无法进行身份验证。

这是我的代码:

client = Google::APIClient.new
client.authorization.access_token = "<MY_CONSOLE_ACCESS_TOKEN>"
client.authorization.scope = "https://www.googleapis.com/auth/shoppingapi"

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

当我查看response.data时,我看到了一条错误消息:

#<Google::APIClient::Schema::Shopping::V1::Products:0x809d6e14 DATA:{"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"authError", "message"=>"Invalid Credentials", "locationType"=>"header", "location"=>"Authorization"}], "code"=>401, "message"=>"Invalid Credentials"}}>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

当然我在发布后几分钟就知道了。我会与其他有同样问题的人分享有用的东西。

client = Google::APIClient.new(authorization: nil)
shopping = client.discovered_api("shopping", "v1")
response = client.execute(key: "<MY_ACCESS_TOKEN>",
                          api_method: shopping.products.list,
                          parameters: { source: "public", country: "US" })