【问题标题】:Getting google +1 Page shares via AJAX (hidden Api)通过 AJAX(隐藏 Api)获取 google +1 页面共享
【发布时间】:2014-02-03 09:51:00
【问题描述】:

努力寻找通过 jQuery - Ajax 从 Google 的隐藏 api 获取 Google Plus +1 of page 的解决方案:https://clients6.google.com/rpc

这个问题也在:Stackoverflow link

我的尝试:

$.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);
    }
});

结果在 chrome 中:Uncaught SyntaxError: Unexpected token :

在 Firefox 中: 语法错误:缺少;声明之前

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

任何想法如何解决这个问题?

【问题讨论】:

    标签: jquery ajax cross-domain google-plus


    【解决方案1】:

    您可以使用 google plus javascript 库来获取分享数:

    包括这些:

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

    然后做:

    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)
    });
    

    不要用您自己的 apikey 替换。如果你这样做是行不通的。

    【讨论】:

    • 感谢您的回复。 gapi.client 应该被初始化吗?我收到一个错误:TypeError: gapi.client is undefined
    • 可以添加回调来知道js库是否已经加载:&lt;script src="https://apis.google.com/js/plusone.js?onload=onLoadCallback"&gt;&lt;/script&gt;。更多信息:developers.google.com/+/web/api/javascript
    • 回调对我也不起作用。出于测试目的,我可以通过手动触发 gapi 调用来使其工作。然而,尽管得到了计数,我也收到了弃用警告。 o,这种方法可能在不久的将来完全停止工作。
    • 此外,Google API 为检索 +1 计数执行了多少请求,这非常丑陋。真丢脸,谷歌!
    【解决方案2】:

    您应该使用 Google+ 的官方 API 来获得页面的 +1 计数。 API 资源管理器中的以下示例显示了 API 调用和响应数据:

    https://developers.google.com/apis-explorer/#p/plus/v1/plus.people.get?userId=%252BGooglePlusDevelopers&fields=plusOneCount&_h=2&

    如何使用 API 客户端库的简短演示:

    1) Google+ 客户端/Google API 客户端库的异步包含

    <script type="text/javascript">
    (function() {
      var po = document.createElement('script');
      po.type = 'text/javascript'; po.async = true;
      po.src = 'https://plus.google.com/js/client:plusone.js';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(po, s);
    })();
    </script>
    

    当客户端加载时,使用来自Google APIs console 的密钥设置 API 密钥:

    gapi.client.setApiKey('YOUR_API_KEY')
    

    接下来,加载回调并在加载客户端后进行 API 调用。

    <script>
    gapi.client.load('plus', 'v1', 
      function(){ 
        gapi.client.plus.people.get(
          {userId: '+GooglePlusDevelopers'}
        ).execute( function(resp){ console.log(resp); }
        );
      });
    </script>
    

    这将返回包含 +1 计数的 JSON 数据,例如:

    gapi.client.load('plus', 'v1', 
      function(){ 
        gapi.client.plus.people.get(
          {userId: '+GooglePlusDevelopers'}
        ).execute( function(resp){ console.log(resp.plusOneCount); }
        );
      });
    

    将返回 225588,即页面的 +1 计数。

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多