使用新的api获取Instagram公共媒体

时间:2017-11-23 22:27:16

标签: javascript node.js get instagram instagram-api

我想在我的nodejs app中获取公开个人资料的10张最新Instagram照片:

const request = require('request');

const accessToken = '1234567890123456789012345678901234567890';

const url = `https://api.instagram.com/v1/users/1470414259/media/recent/?access_token=${accessToken}&count=10`;

request.get(url, { json: true }, (err, res, body) => {

console.log('print the body: ', body);
});

我的尝试没有成功,我没有得到任何结果。 有人可以帮我解决这个问题吗?

我正在尝试按照此处的示例:https://www.instagram.com/developer/endpoints/users/#get_users_media_recent

更新

我将用户名更新为用户ID,但我仍然没有得到回复:

{ meta: 
   { code: 400,
     error_type: 'APINotFoundError',
     error_message: 'this user does not exist' } }

更新2:

实际上我意识到我的访问令牌有一些限制:

{"meta": {"code": 400, "error_type": "OAuthPermissionsException", "error_message": "This request requires scope=public_content, but this access token is not authorized with this scope. The user must re-authorize your application with scope=public_content to be granted this permissions."}}

为什么会这样?我只想尝试一些公开的个人资料。

更新3:

有没有其他方法可以绕过这些访问令牌权限,只获取公共用户的10个媒体?

2 个答案:

答案 0 :(得分:1)

看看这个链接返回的json: https://www.instagram.com/aa/?__a=1

  body.user.media[0] will give you the last photo object
  body.user.media[1] will give you the photo before the last one object

..等等。

您必须在此处更改我提供给您所需用户名的网址中的aa

PS。如果浏览器不支持

,你可以使用一些json viewer工具来组织json

答案 1 :(得分:0)

你有两个问题:

首先,端点接收用户ID作为param(而不是用户名):

const url = `https://api.instagram.com/v1/users/${userId}/media/recent/?access_token=${accessToken}&count=10`;

除此之外,端点还会响应一个只有一个元素的对象:data,即一个数组。所以我不知道你想要获得什么数据,但是你不能在body.user得到它,你应该通过数组并要求用户处于相应的位置,例如尝试这个:console.log(body.data[0].user)

您当然必须定义有效的访问令牌!