2012-12-07 97 views
0

我正嘗試使用google-api-client寶石來訪問Google Search API for Shopping。不幸的是,儘管通過Google APIs Console創建了access_token,但我似乎無法進行身份驗證。訪問Google API for Shopping時無效的憑證

這裏是我的代碼:

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

當然,我在發佈後的數分鐘內就知道了。我會分享對於有同樣問題的其他人有效的東西。

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

非常感謝那個真正幫助我的答案,但是您的意思是API Access Key由「Access Token」嗎? – Codii