通过Javascript使用“获取”方法获取Google+点数/份额数

时间:2014-08-13 13:28:18

标签: javascript google-plus social

如何使用" get"来获取通过Javascript(整数)的Google+计数数量?

在Twitter中例如,我们可以使用URL获取: http://urls.api.twitter.com/1/urls/count.json?url=' + url

对于Google+股票,我发现我们可以获得整个g +图标和使用的股票数量 https://plusone.google.com/_/+1/fastbutton?url=网址

另外,我找到了一种通过发出AJAX请求来获取G +计数的方法

如何通过" get"。

将其作为数字值

1 个答案:

答案 0 :(得分:0)

我强烈建议您不要像使用原始API查询一样。生成一个简单的API密钥,可以访问Google Developers Console上的Google+ API。

您可以找到指定网址中的帖子,并按如下方式获取活动详情,请注意我使用Google API client for JavaScript

/**
 * Performs the API queries for searching,
 *
 * @param {String} searchUrl The Url containing the user ID for searching.
 * @param {int} queryCount The number of API calls made.
 * @param {String} nextPageToken The next page token for paged api calls.
 */
function searchForUrl(searchUrl, queryCount, nextPageToken){
  var userId = searchUrl.split('/')[3];

  gapi.client.plus.activities.list({userId: userId,
      pageToken: nextPageToken}).execute( function(resp){
        handleActivities(resp.items, searchUrl, queryCount, resp.nextPageToken);
      });
}


/**
 * Parses and stores activities from the XMLHttpRequest
 *
 * @param {Object} activities The response activity objects as an array.
 * @param {String} postUrl The URL of the post.
 * @param {int} queryCount The number of API calls.
 * @param {string} nextPageToken The next page token.
 */
function handleActivities(activities, postUrl, queryCount, nextPageToken){
  for (var activity in activities){
    activity = activities[activity];
    if (activity['url'] == postUrl){
      targetActivity = activity;
      document.getElementById('result').value = 'ID is: ' +
          activity.id + '\n' +
          'PlusCount is: ' + activity.object.plusoners.totalItems + '\n' +
          'Replies are: ' + activity.object.replies.totalItems + '\n' +
          'Reshares are: ' + activity.object.resharers.totalItems + '\n';
      isFound = true;
    }else{
      console.log(activity);
    }
  }

  if (queryCount < maxQueryCount && !isFound){
    queryCount++;

    // throttle calls using timer to avoid reaching query limit
    console.log('retrying with ' + nextPageToken);
    setTimeout(searchForUrl(postUrl, queryCount, nextPageToken), 100);
  }
}

http://wheresgus.com/urltoid/

演示