Вопрос

I'm trying to use the google-api-client gem to access the Google Search API for Shopping. Unfortunately, I can't seem to authenticate despite creating an access_token through the Google APIs Console.

Here is my code:

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" })

When I view response.data, I see an error message though:

#<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"}}>

What am I doing wrong?

Это было полезно?

Решение

Of course I figure it out minutes after posting. I'll share what worked for anyone else who has the same question.

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" })
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top