0

我正在尝试使用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"}}>

我究竟做错了什么?

4

1 回答 1

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" })
于 2012-12-07T17:33:37.503 回答