3

我需要找到 Google 的计数加一作为 url。为了实现这一点,我正在关注这个博客

require 'curb'
require 'json'
hash = {method: "pos.plusones.get",id: "p",params: {nolog: true,id: "http://www.google.com",source: "widget",userId: "@viewer",groupId: "@self"},jsonrpc: "2.0",key: "p",apiVersion: "v1"}
 result=JSON.parse((Curl::Easy.http_post("https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ", hash.to_json ) do |curl|
    curl.headers['Accept']='application/json'
    curl.headers['Content-Type']='application/json'
    curl.headers['Api-Version']='2.2'
  end
).body_str)
p result
count = result && result['result'] && result['result']['metadata'] && result['result']['metadata']['globalCounts'] && result['result']['metadata']['globalCounts']['count'] ? result['result']['metadata']['globalCounts']['count'] : 0
count ? count : 0

但是当我运行上面的代码时,它给了我以下错误

{"error"=>{"code"=>400, "message"=>"Invalid Value", "data"=>[{"domain"=>"global", "reason"=>"invalid", "message"=>"Invalid Value"}]}, "id"=>"p"}

当我跑步时

curl -i -H 'Accept: application/json' -H 'Content-type: application/json' -X POST -d "{\"method\":\"pos.plusones.get\",\"id\":\"p\",\"params\":{\"nolog\":true,\"id\":\"http://www.google.com\",\"source\":\"widget\",\"userId\":\"@viewer\",\"groupId\":\"@self\"},\"jsonrpc\":\"2.0\",\"key\":\"p\",\"apiVersion\":\"v1\"}"  'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ'

我明白了

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Wed, 12 Dec 2012 11:52:58 GMT
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked

{
 "error": {
  "code": 400,
  "message": "Invalid Value",
  "data": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value"
   }
  ]
 },
 "id": "p"
}
4

1 回答 1

3

以下代码适用于红宝石

    require 'curb'
    require 'json'
    hash =  {method: "pos.plusones.get",id: "p",params: {nolog: true, id: "http://www.tomanthony.co.uk/", source: "widget", userId: "@viewer", groupId: "@self"}, jsonrpc: "2.0", key: "p", apiVersion: "v1"} 
    result=(Curl::Easy.http_post("https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ", 
hash.to_json) do |curl|
      curl.headers['Accept']='application/json'
      curl.headers['Content-Type']='application/json'
      curl.headers['Api-Version']='2.2'
    end)
    result=JSON.parse(result.body_str)
    count = result && result['result'] && result['result']['metadata'] && result['result']['metadata']['globalCounts'] && result['result']['metadata']['globalCounts']['count'] ? result['result']['metadata']['globalCounts']['count'].to_i : 0
于 2012-12-12T13:13:03.967 回答