GMail API分页使用nextPageToken [英] GMail API pagination use of nextPageToken

查看:588
本文介绍了GMail API分页使用nextPageToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了几个小时搜索Google等.等要获得答案,请确保它很简单,但是如何使用nextPageToken使用GMail api创建分页?我所做的一切都无法使分页工作(就是来回).

Have spent hours searching Google et. al. for an answer, sure it's simple but how do you create pagination with the GMail api using nextPageToken? what ever I do cannot get pagination to work (back and forth that is).

假设授权用户"并以我称呼的正确范围进行访问

Assume 'authorised user' and access with the right scopes I call

 gapi.client.load('gmail','v1',displayInbox);

然后

function displayInbox(){
    var request = gapi.client.gmail.users.messages.list({
    'userId':'me',
    'maxResults':10,
    });

  request.execute(function(response){
    $.each(response.messages,function(){
      var messageRequest = gapi.client.gmail.users.messages.get({
        'userId':'me',
        'id':this.id
      });
      messageRequest.execute(appendMessageRow);
    });
  });
}

appendMessageRow只是将列表布置在表格中,例如

appendMessageRow simply lays out the list in a table e.g.

function appendMessageRow(message){
   var txt = '<tr>';
   txt +='<td>'+getHeader(message.payload.headers, 'From')+'</td>';
   txt +='<td>';
   txt +='<a href="#message-modal-'+ message.id +'" data-toggle="modal" id="message-link-' + message.id+'">' +getHeader(message.payload.headers, 'Subject') +'</a>';
   txt +='</td>';
   txt +='<td class="text-xs-right">'+moment(parseInt(message.internalDate)).format('HH:mm')+'</td>';
   txt +='</tr>';
   $('table tbody').append(txt);
}

当我console.log request.execute时,我看到nextPageToken作为对象键,我不能做并且需要做的是添加分页按钮-messageRequest.execute无法通过nextPageToken加上似乎没有一种创建方法/获取"previousPageToken".

When I console.log request.execute I see nextPageToken as an object key What I cannot do and need to do is add pagination buttons - messageRequest.execute does not pass the nextPageToken plus there does not seem to be a way to create/obtain a 'previousPageToken'.

很抱歉,如果很简单,但这是我还是比这还远不止于此? (对我来说)GMail API文档在这个问题上显得很差,我还没有找到一个有帮助的stackoverflow答案.

Sorry if simple but is it me or is there far more to it than that? The GMail API docs appear very poor (to me) on this subject and I have not found a stackoverflow answer that helps.

回顾一下-如何添加分页按钮并将适当的变量传递给调用/调用displayInbox().

To recap - how do I add pagination buttons and pass the appropriate variables to call/recall displayInbox().

预先感谢

推荐答案

您可以在每个请求上保存下一个页面令牌,并在下一个请求中使用它.如果响应中没有下一个页面令牌,则说明您已收到所有消息:

You could save the next page token on every request and use it in your next request. If there is no next page token in the response, you know that you have gotten all messages:

function listMessages(pageToken) {
  return new Promise(function(resolve) {
    var options = {
      userId: 'me',
      maxResults: 10
    };
    if (pageToken) {
      options.pageToken = pageToken;
    }
    var request = gapi.client.gmail.users.messages.list(options);
    request.execute(resolve);
  });
}

function getMessage(message) {
  return new Promise(function(resolve) {
    var messageRequest = gapi.client.gmail.users.messages.get({
      userId: 'me',
      id: message.id
    });
    messageRequest.execute(resolve);
  });
}

var pageToken;
function displayInbox(){
  listMessages(pageToken).then(function (response) {
    if (response.nextPageToken) {
      pageToken = response.nextPageToken; // Get the next page next time
    } else {
      console.log('No more pages left!');
    }
    if (response.messages) {
      Promise.all(response.messages.map(getMessage)).then(function (messages) {
        messages.forEach(appendMessageRow);
      });
    }
  })
}

这篇关于GMail API分页使用nextPageToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆