Skip to content

Instantly share code, notes, and snippets.

@daliborgogic

daliborgogic/instagram.js

Last active Oct 4, 2019
Embed
What would you like to do?
Instagram Graph API Media
const fetch = require('node-fetch')
const { FB_PAGE_ID, FB_ACCESS_TOKEN } = process.env
const { getParams } = require('./helpers')
const GRAPH_URL = 'https://graph.facebook.com/v4.0'
const headers = { 'content-type': 'application/json' }
const options = { headers }
let params = { access_token: FB_ACCESS_TOKEN }
async function getIDs () {
const ids = new URL(`${GRAPH_URL}/${FB_PAGE_ID}/media`)
await getParams(params, ids)
return await(await fetch(ids, options)).json()
}
async function feed () {
let images = []
const { data } = await getIDs()
params.fields = 'media_type,media_url'
for (const { id } of data) {
const imageUrl = new URL(`${GRAPH_URL}/${id}`)
await getParams(params, imageUrl)
const image = await(await fetch(imageUrl, options)).json()
if (image.media_type === 'IMAGE') images = [...images, image]
}
return images
}
module.exports = { feed }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.