0

我已将 maxResults 设置为 10,我想知道如何使用 nextpageToken 查看更多圈子,然后通过单击查看更多按钮,接下来的 10 个圈子必须显示并一直持续到 Google+ 中的最后一个圈子。请帮我解决这个问题。请在下面查看我的代码:

<html>
<head>
  <title>Google+ JavaScript Quickstart</title>

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


  <!-- JavaScript specific to this application that is not related to API
     calls -->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" ></script>


</head>
<body>
  <div id="gConnect">
    <button class="g-signin"
        data-scope="https://www.googleapis.com/auth/plus.login"
        data-requestvisibleactions="http://schemas.google.com/AddActivity"
        data-clientId="my client id"
        data-callback="onSignInCallback"
        data-theme="dark"
        data-cookiepolicy="single_host_origin">
    </button>
  </div>
  <div id="authOps" style="display:none">
    <h2>User is now signed in to the app using Google+</h2>
    <p>If the user chooses to disconnect, the app must delete all stored
    information retrieved from Google for the given user.</p>

    <button id="disconnect" >Disconnect your Google account from this app</button>

    <h2>User's profile information</h2>
    <div id="profile"></div>

    <h2>User's friends that are visible to this app</h2>
    <div id="visiblePeople"></div>

    <p><a href="#" id="getMore" onClick="getMore()">View More</a></p>
    <h2>Authentication Logs</h2>
    <pre id="authResult"></pre>
  </div>
</body>
<script type="text/javascript">
var helper = (function() {
  var BASE_API_PATH = 'plus/v1/';

  return {
    /**
     * Hides the sign in button and starts the post-authorization operations.
     *
     * @param {Object} authResult An Object which contains the access token and
     *   other authentication information.
     */
    onSignInCallback: function(authResult) {
      gapi.client.load('plus','v1', function(){
        $('#authResult').html('Auth Result:<br/>');
        for (var field in authResult) {
          $('#authResult').append(' ' + field + ': ' +
              authResult[field] + '<br/>');
        }
        if (authResult['access_token']) {
          $('#authOps').show('slow');
          $('#gConnect').hide();
          helper.profile();
          helper.people();
        } else if (authResult['error']) {
          // There was an error, which means the user is not signed in.
          // As an example, you can handle by writing to the console:
          console.log('There was an error: ' + authResult['error']);
          $('#authResult').append('Logged out');
          $('#authOps').hide('slow');
          $('#gConnect').show();
        }
        console.log('authResult', authResult);
      });
    },

    /**
     * Calls the OAuth2 endpoint to disconnect the app for the user.
     */
    disconnect: function() {
      // Revoke the access token.
      $.ajax({
        type: 'GET',
        url: 'https://accounts.google.com/o/oauth2/revoke?token=' +
            gapi.auth.getToken().access_token,
        async: false,
        contentType: 'application/json',
        dataType: 'jsonp',
        success: function(result) {
          console.log('revoke response: ' + result);
          $('#authOps').hide();
          $('#profile').empty();
          $('#visiblePeople').empty();
          $('#authResult').empty();
          $('#gConnect').show();
        },
        error: function(e) {
          console.log(e);
        }
      });
    },

    /**
     * Gets and renders the list of people visible to this app.
     */
    people: function() {
      var request = gapi.client.plus.people.list({

        'userId': 'me',
        'collection': 'visible',
        'selfLink':'http://localhost/Google+/trail+.html',
        'maxResults':10,`enter code here`
        'items[]' : 'list',
        'nextPageToken': 'CAIQ0K3cq5DEtAIgAygB'


              });
      request.execute(function(people) {
        $('#visiblePeople').empty();
        $('#visiblePeople').append('Number of people visible to this app: ' +
            people.totalItems + '<br/>');




        for (var personIndex in people.items) {
          person = people.items[personIndex];



          $('#visiblePeople').append('<img src="' + person.image.url + '">');
           $('#visiblePeople').append(''+ person.displayName + '</br>'+ '</br>');



        }
      });
    },

    /**
     * Gets and renders the currently signed in user's profile data.
     */
    profile: function(){
      var request = gapi.client.plus.people.get( {'userId' : 'me'} );
      request.execute( function(profile) {
        $('#profile').empty();
        if (profile.error) {
          $('#profile').append(profile.error);
          return;
        }
        $('#profile').append(
            $('<p><img src=\"' + profile.image.url + '\"></p>'));
        $('#profile').append(
            $('<p>Hello ' + profile.displayName + '!<br />Tagline: ' +profile.tagline + '!<br />Email id: ' +profile.email +
            + '<br />About: ' + profile.aboutMe + '</p>'));
        if (profile.cover && profile.coverPhoto) {
          $('#profile').append(
              $('<p><img src=\"' + profile.cover.coverPhoto.url + '\"></p>'));

        }
      });
    }
  };
})();

/**
 * jQuery initialization
 */
$(document).ready(function() {
  $('#disconnect').click(helper.disconnect);
  if ($('[data-clientid="YOUR_CLIENT_ID"]').length > 0) {
    alert('This sample requires your OAuth credentials (client ID) ' +
        'from the Google APIs console:\n' +
        '    https://code.google.com/apis/console/#:access\n\n' +
        'Find and replace YOUR_CLIENT_ID with your client ID.'
    );
  }


  });

/**
 * Calls the helper method that handles the authentication flow.
 *
 * @param {Object} authResult An Object which contains the access token and
 *   other authentication information.
 */
function onSignInCallback(authResult) {
  helper.onSignInCallback(authResult);
}
function getMore()
{
helper.people();

}
</script>
</html>
4

1 回答 1

3

看起来这里可能有几个问题。您似乎至少了解了如何使用 people.list 的一些要点,但您似乎正在尝试将一些响应字段添加到请求字段。有关请求参数和预期响应的完整详细信息,请参阅https://developers.google.com/+/api/latest/people/list

在第一次调用时,您只需要传递userIdcollection参数。由于您想限制页面大小,maxResults因此您也需要通过,因此您的调用看起来像

var requestParams = {
  'userId': '我',
  “收藏”:“可见”,
  “最大结果”:10
};
gapi.client.plus.people.list(requestParams).execute(peopleCallback);

传递给的参数peopleCallback()将包含结果,包括 a nextPageToken,您需要将其传递给后续调用以获取其他项目。您可以将此标记存储在全局变量中,或作为辅助对象中的属性,并处理您需要的其他字段。所以它可能看起来像这样:

peopleCallback:函数(响应){
  nextPageToken = response.nextPageToken;
  items.forEach(功能(项目){
    $('#visiblePeople').append(''+person.displayName+'');
  });
}

下次调用“列出更多”按钮时,您需要将 nextPageToken 作为请求的一部分包含在内,因此调用看起来更像这样:

var requestParams = {
  'userId': '我',
  “收藏”:“可见”,
  “最大结果”:10,
  'pageToken':下一个PageToken
};
gapi.client.plus.people.list(requestParams).execute(peopleCallback);

后续调用将获得一个新的 nextPageToken ,下次您调用以继续获取列表时应将其传递。

我把它作为一个练习来确定您处理这些调用之间差异的最佳方法(传递令牌与不传递)、初始化可见人员列表以及特定于您的代码结构的其他问题。

然而,关于你想要做什么的一个重要警告。您的问题标题表明您正在尝试获取某个人的圈子 - 这不会为您提供该信息。people.list 调用将给您添加一个人愿意公开表示的人,而不是圈子。用户可能选择不提供此信息,或者可能选择仅提供信息的子集,您将不知道某个人可能已添加到哪些具体命名的圈子

于 2013-08-31T15:30:20.360 回答