Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jquery - Getting google +1 Page shares via AJAX (hidden Api)

Struggling to find the solution for getting the Google Plus +1 of page via jQuery - Ajax from Google's hidden api: https://clients6.google.com/rpc

This issue is also discussed at: Stackoverflow link

My attempt:

$.ajax({
    cache: false,
    type: "POST",
    url: "https://clients6.google.com/rpc",
    data: [{
        "method":"pos.plusones.get",
        "id":"p",
        "params":{
            "nolog":true,
            "id":"http://www.apple.com",
            //"id":"http%3A%2F%2Fwww.apple.com",
            "source":"widget",
            "userId":"@viewer",
            "groupId":"@self"
            },
            "jsonrpc":"2.0",
            "key":"p",
            "apiVersion":"v1"
    }],
    crossDomain: true,
    jsonp: true,
    timeout: 5000,
    dataType: "jsonp",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        console.log(data);
    },
    always: function(data){
        console.log(data);
    }
});

With result in chrome: Uncaught SyntaxError: Unexpected token :

And in Firefox: SyntaxError: missing ; before statement

{"error":{"code":-32700,"message":"Parse Error","data":[{"domain":"g

Any ideas how to solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the google plus javascript library to get the share count:

Include these:

<script src="https://apis.google.com/js/plusone.js"></script>
<script src="https://apis.google.com/js/client:plusone.js"></script>

Then do:

var params = {
  nolog: true,
  id: "http://www.google.com/",
  source: "widget",
  userId: "@viewer",
  groupId: "@self"
};

gapi.client.setApiKey('AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ')
gapi.client.rpcRequest('pos.plusones.get', 'v1', params).execute(function(resp) {
  console.log('count:', resp.result.metadata.globalCounts.count)
});

Don't replace the apikey with your own. If you do it won't work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...