Last active
October 4, 2019 10:27
-
-
Save daliborgogic/bcf03ba3e393d4702859a7c3ef9abb60 to your computer and use it in GitHub Desktop.
Instagram Graph API Media
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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