Instagram API分页 - 上一页

tya*_*ird 4 javascript jquery instagram instagram-api

我正在使用Jquery获得Instagram用户的媒体.

我需要获取所有用户媒体,但Instagram API媒体限制仅为20,API分页仅返回下一个链接.之前没有链接.

我写代码有点想法,这是下一个工作,但以前没有工作.我在这里堆叠

function instaPhotos(page) {


var $limit = 1;
var $token = $('.instagram').data('instagramtoken');
var $nextId = $('.instagram').attr('data-instagramnext');
var $pageNumber = parseInt(localStorage.getItem('page'));


switch (page){      
    case 'next':

        localStorage.setItem('page', ($pageNumber+1));
        var $url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit + '&max_id=' + $nextId


    break;      
    case 'prev':

        localStorage.setItem('page', ($pageNumber-1));
        var $url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit + '&min_id=' + $nextId

    break;
    default:

        localStorage.setItem('page', 1);        
        var $url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit

}


console.log($pageNumber);
console.log($url);

$.ajax({
    method: "GET",
    url: $url,
    dataType: "jsonp",
    context: this,
    success: function (r) {

        if (r.pagination.next_max_id){
            $('.instagram').attr('data-instagramnext', r.pagination.next_max_id);
        }

        // photo actions here.
    }

});

}
Run Code Online (Sandbox Code Playgroud)

Léo*_*niz 6

首先:您可以将媒体限制增加到最多33张照片.您应该在查询字符串上使用count参数:

https://api.instagram.com/v1/tags/nofilter/media/recent?count=33&access_token=YOUR_ACCESS_TOKEN

即将导航到上一页,在我看来Instagram的响应将始终与最新发布的照片​​.

来自Instagram API:

MIN_TAG_ID  Return media before this min_tag_id.
MAX_TAG_ID  Return media after this max_tag_id.
Run Code Online (Sandbox Code Playgroud)

所以让我们想一想3页(从id#1到id#100):

第一页:

next_min_id #0
photo #1
photo #2
photo #3
...
photo #33
next_max_tag_id #34
Run Code Online (Sandbox Code Playgroud)

第二页:

next_min_id #34 (equals to next_max_tag_id of first page) 
photo #35
photo #36
photo #37
... 
photo #66
next_max_tag_id #67
Run Code Online (Sandbox Code Playgroud)

第三页:

next_min_id #67 (equals to next_max_tag_id of second page)
photo #68
photo #69
photo #70
...
photo #99
next_max_tag_id #100
Run Code Online (Sandbox Code Playgroud)

虽然查询字符串参数max_tag_id限制了结果的顶部,但min_tag_id限制了它的底部.因此,如果仅使用min_tag_id作为参数,则响应将等于第一页,尽管页面的min_tag_id值来自.在第一页的所有照片(#1,#2,#3 ......)出现在任何其他页面(#34和#67)的min_tag_id之前,并且是最新的页面.

我认为完成此任务的唯一方法是使用max_tag_id查询字符串参数或next_url结果(整个网址随时可用 - 更容易).

您应该保存或者max_tag_id或者(甚至更好,更快)整个结果并控制代码上的逻辑.工作范例:

var token = "<your_access_token>";
var nextURL = "https://api.instagram.com/v1/tags/riodejaneiro/media/recent?count=33&access_token=" + token;

var currentPage = 0;
var pages = new Array();

$(function() { requestInstagram(); });

function previousPage() {

   currentPage = (currentPage>0) ? currentPage-1 : 0;
   // show photos of currentPage

   console.log("Page " + currentPage);
   console.log("First photo:" + pages[currentPage][0].id);

 }

 function nextPage() { 

    if (++currentPage < pages.length ) {
       // show photos of currentPage
       console.log("Page " + currentPage);
       console.log("First photo:" + pages[currentPage][0].id);
    } else {
       requestInstagram(); 
    }
}

function requestInstagram() { 
 $.ajax({
    method: "GET",
    url: nextURL ,
    dataType: "jsonp",
    context: this,
    success: function (r) {

      if (r.meta.code == "200") {

        pages[pages.length] = r.data;
        // show photos here
        console.log("Page " + currentPage);
        console.log("First photo:" + pages[currentPage][0].id);

        nextURL = r.pagination.next_url; // you should implement a way to identify the last page
        console.log(nextURL);

      }
    }

  });

}
Run Code Online (Sandbox Code Playgroud)

祝好运!