问题
I am trying to get Google plus shares count for a URL with Jquery.
url = 'www.abc.com/';
$.getJSON('https://clients6.google.com/rpc?key=AIzaSyBeAeOUk3BJs9ZXeohJX6Qt2zWXPQsRKRk'+'callback=?',
{
"method":"pos.plusones.get",
"id":"p",
"params":{
"nolog":true,
"id":'http://prince-antil.myshopify.com/',
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"p",
"apiVersion":"v1",
},
function(data){
plusones = data.count;
alert(data);
});
But I am getting following error:
{
"error": {
"code": 400,
"message": "Bad Request",
"data": [
{
"domain": "usageLimits",
"reason": "keyInvalid",
"message": "Bad Request"
}
]
},
"id": "p"
}
I have created an API but I am not sure about the way I have created it. Above error says "invalid key". I have cross checked the key but the key is correct. I don't know if there is something special need to configure in the APP area. I am new bie. Please help me out.
Thanks.
回答1:
Took some work but this solution did the trick. (Tested in Firefox and Chrome) No API Key required and no changes necessary. :) Don't forget to include your jQuery file.
$(window).load(function(){
/* Social Share: Google Plus JSON */
var data = {
"method":"pos.plusones.get",
"id":"http://www.website_you_want_share_count_for.com",
"params":{
"nolog":true,
"id":"http://www.website_you_want_share_count_for.com",
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"p",
"apiVersion":"v1"
};
$.ajax({
type: "POST",
url: "https://clients6.google.com/rpc",
processData: true,
contentType: 'application/json',
data: JSON.stringify(data),
success: function(r){
$('#googleplus_page_share_count').text(format(r.result.metadata.globalCounts.count));
}
});
});
<p><span id="googleplus_page_share_count">256</span> Shares.</p>
回答2:
The key "p"
is certainly not correct.
You've given a more realistic key in the URL, but overwritten it with this dummy key.
Put it in the data array instead:
$.getJSON('https://clients6.google.com/rpc?callback=?',
{
"method":"pos.plusones.get",
"id":"p",
"params":{
"nolog":true,
"id":'http://prince-antil.myshopify.com/',
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"AIzaSyBeAeOUk3BJs9ZXeohJX6Qt2zWXPQsRKRk",
"apiVersion":"v1",
},
function(data){
plusones = data.count;
alert(data);
}
);
来源:https://stackoverflow.com/questions/20544462/getting-google-plus-shares-count-with-jquery