Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active October 4, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daliborgogic/bcf03ba3e393d4702859a7c3ef9abb60 to your computer and use it in GitHub Desktop.
Save daliborgogic/bcf03ba3e393d4702859a7c3ef9abb60 to your computer and use it in GitHub Desktop.
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