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

后端 未结 2 946
暗喜
暗喜 2020-12-18 14:34

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

T

相关标签:
2条回答
  • 2020-12-18 15:05

    You should be using the official API for Google+ to get +1 counts for pages. The following example from the APIs explorer shows the API call and response data:

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

    A short demo of how to use the API client library:

    1) Async include of the Google+ client / Google API client library

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

    When the client loads, set the API key with a key from the Google APIs console:

    gapi.client.setApiKey('YOUR_API_KEY')
    

    Next, load the callback and place an API call once the client is loaded.

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

    This will return JSON data that includes the +1 count, e.g.:

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

    Will return 225588, the count of +1s for the page.

    0 讨论(0)
  • 2020-12-18 15:27

    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.

    0 讨论(0)
提交回复
热议问题