Instagram API:如何获取所有用户媒体? [英] Instagram API: How to get all user media?

查看:28
本文介绍了Instagram API:如何获取所有用户媒体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,我需要获取所有用户媒体.

In general I need to get all user media.

用户拥有超过 250 张照片.

User has more than 250 photos.

/users/1/media/recent/?access_token=...&count=250

但它只返回 20 张照片.

But it returns only 20 photos.

也许 Instagram 有获取媒体的限制.如果是,响应有一个分页来解决它.但只有最大身份证照片.那么如何知道第一张(最小)身份证照片以对其进行分页?

Maybe instagram has a limit for getting media. If it is, response has a pagination to solve it. But there are only max ID photo. How to know the first (min) ID photo to paginate it then?

推荐答案

没错,Instagram API 每次调用只会返回 20 张图片.所以你必须使用分页功能.

You're right, the Instagram API will only return 20 images per call. So you'll have to use the pagination feature.

如果您尝试使用 API 控制台.您首先需要允许 API 控制台通过您的 Instagram 登录进行身份验证.为此,您需要在身份验证"下拉列表下选择 OAUTH2.

If you're trying to use the API console. You'll want to first allow the API console to authenticate via your Instagram login. To do this you'll want to select OAUTH2 under the Authentication dropdown.

经过身份验证后,使用左侧菜单选择 users/{user-id}/media/recent 端点.因此,对于 {user-id} 的这篇文章,您可以将其替换为 self.然后,这将使用您的帐户来检索信息.

Once Authenticated, use the left hand side menu to select the users/{user-id}/media/recent endpoint. So for the sake of this post for {user-id} you can just replace it with self. This will then use your account to retrieve information.

至少需要为此端点执行 GET 操作.一旦你发送,你会得到一些 json 返回给你.在所有服务器信息之后返回的信息的最顶部,您将看到带有 next_url 和 next_max_id 的分页部分.

At a bare minimum that is what's needed to do a GET for this endpoint. Once you send, you'll get some json returned to you. At the very top of the returned information after all the server info, you'll see a pagination portion with next_url and next_max_id.

next_max_id 是您将用作查询参数的内容.记住 max_id 是最早返回的 20 个图像中最早的图像的 id.这将用于返回早于此图像的图像.

next_max_id is what you'll use as a parameter for your query. Remember max_id is the id of the image that is the oldest of the 20 that was first returned. This will be used to return images earlier than this image.

如果您不想,您不必使用 max_id.实际上,您可以获取想要开始查询更多图像的图像的 id.

You don't have to use the max_id if you don't want to. You can actually just grab the id of the image where you'd like to start querying more images from.

所以从返回的数据中,将max_id复制到参数max_id中.请求 URL 应如下所示 https://api.instagram.com/v1/users/self/media/recent?max_id=XXXXXXXXXXX 其中 XXXXXXXXXXX 是 max_id.再次点击发送,你应该会得到接下来的 20 张照片.

So from the returned data, copy the max_id into the parameter max_id. The request URL should look something like this https://api.instagram.com/v1/users/self/media/recent?max_id=XXXXXXXXXXX where XXXXXXXXXXX is the max_id. Hit send again and you should get the next 20 photos.

从那里您还会收到更新的 max_id.然后您可以再次使用它来获取下一组 20 张照片,直到最终浏览用户的所有照片.

From there you'll also receive an updated max_id. You can then use that again to get the next set of 20 photos until eventually going through all of the user's photos.

我在我正在进行的项目中所做的是加载从最初的最近媒体请求返回的前 20 张照片.然后,我为图像分配一个数据 ID(-id 实际上可以是您想要的任何内容).然后在照片集底部添加了一个加载更多按钮.

What I've done in the project I'm working on is to load the first 20 photos returned from the initial recent media request. I then, assign the images with a data-id (-id can actually be whatever you'd like it to be). Then added a load more button on the bottom of the photo set.

当按钮被点击时,我使用 jQuery 来抓取最后一张图片和它的 data-id 属性,并使用它通过 ajax 创建一个 get 调用并将结果附加到页面上已经存在的照片的末尾.您可以将其替换为具有无限滚动效果的按钮,而不是按钮.

When the button is clicked, I use jQuery to grab the last image and it's data-id attribute and use that to create a get call via ajax and append the results to the end of the photos already on the page. Instead of a button you could just replace it to have a infinite scrolling effect.

希望有所帮助.

这篇关于Instagram API:如何获取所有用户媒体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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