gpt4 book ai didi

ruby - 如何使用 rails 仅查找 google 加一计数(不需要按钮和 g +1 图像)

转载 作者:数据小太阳 更新时间:2023-10-29 08:00:47 26 4
gpt4 key购买 nike

我需要为某个网址查找 Google 加一的计数。为了实现这一点,我正在关注这个 blog

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

最佳答案

以下代码适用于 ruby​​

    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

关于ruby - 如何使用 rails 仅查找 google 加一计数(不需要按钮和 g +1 图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13836598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com