Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Instagram): Update Instagram oEmbed endpoint #148

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,39 @@ https://instagram.com/p/B60jPE6J8U-

</details>

#### Options

All options should go under the `Instagram` namespace.

| name | Type | Required | Default | Description |
| ----------- | -------- | -------- | ------- | -------------------------------------------------------- |
| accessToken | `string` | ✅ | | An App Access Token (recommended) or Client Access Token |

##### accessToken

To get an App Access Token (recommended) or Client Access Token for the
Instagram embed, check out the [Instagram oEmbed access token
docs][instagram-oembed-access-token-docs] and
[requirements][instagram-oembed-requirements-docs].

The safest way to enter your `accessToken` is to set is as an [environment
variable][gatsby-environment-variables-docs].

<details>
<summary><b>Example</b></summary>

```js
const GatsbyRemarkEmbedderOptions = {
services: {
Instagram: {
accessToken: env.process.INSTAGRAM_ACCESS_TOKEN,
},
},
};
```

</details>

### Lichess

#### Usage
Expand Down Expand Up @@ -863,6 +896,7 @@ MIT
[codesandbox]: https://codesandbox.io
[embedded-tweet-docs]: https://developer.twitter.com/web/embedded-tweets
[gatsby]: https://github.com/gatsbyjs/gatsby
[gatsby-environment-variables-docs]: https://www.gatsbyjs.com/docs/environment-variables
[gatsby-https-docs]: https://gatsbyjs.org/docs/local-https
[gatsby-plugin-instagram-embed]: https://github.com/MichaelDeBoey/gatsby-plugin-instagram-embed
[gatsby-plugin-mdx]: https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-mdx
Expand All @@ -871,6 +905,8 @@ MIT
[gatsby-transformer-remark]: https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-remark
[giphy]: https://giphy.com
[instagram]: https://instagram.com
[instagram-oembed-access-token-docs]: https://developers.facebook.com/docs/instagram/oembed#access-tokens
[instagram-oembed-requirements-docs]: https://developers.facebook.com/docs/instagram/oembed#requirements
[kentcdodds.com-repo]: https://github.com/kentcdodds/kentcdodds.com
[lichess]: https://lichess.org
[netlify-environment-variables-docs]: https://docs.netlify.com/configure-builds/environment-variables/#deploy-urls-and-metadata
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe('gatsby-remark-embedder', () => {
{ cache, markdownAST },
{
services: {
Instagram: {
accessToken: 'access-token',
},
Twitch: {
parent: 'embed.example.com',
},
Expand Down
15 changes: 13 additions & 2 deletions src/__tests__/transformers/Instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ test('Gets the correct Instagram iframe', async () => {
`<blockquote class="instagram-media-mocked-fetch-transformer"><div><a href="https://instagram.com/p/B60jPE6J8U-"><p>example</p></a><p>A post shared by <a href="https://instagram.com/michaeldeboey">Michaël De Boey</a> (@michaeldeboey) on<timedatetime="2020-01-02T14:45:30+00:00">Jan 2, 2020 at 6:45am PST</time></p></div></blockquote>`
);

const html = await getHTML('https://instagram.com/p/B60jPE6J8U-');
const html = await getHTML('https://instagram.com/p/B60jPE6J8U-', {
accessToken: 'access-token',
});

expect(html).toMatchInlineSnapshot(
`"<blockquote class=\\"instagram-media-mocked-fetch-transformer\\"><div><a href=\\"https://instagram.com/p/B60jPE6J8U-\\"><p>example</p></a><p>A post shared by <a href=\\"https://instagram.com/michaeldeboey\\">Michaël De Boey</a> (@michaeldeboey) on<timedatetime=\\"2020-01-02T14:45:30+00:00\\">Jan 2, 2020 at 6:45am PST</time></p></div></blockquote>"`
Expand All @@ -117,7 +119,16 @@ test('Plugin can transform Instagram links', async () => {
);
const markdownAST = getMarkdownASTForFile('Instagram');

const processedAST = await plugin({ cache, markdownAST });
const processedAST = await plugin(
{ cache, markdownAST },
{
services: {
Instagram: {
accessToken: 'access-token',
},
},
}
);

expect(parseASTToMarkdown(processedAST)).toMatchInlineSnapshot(`
"<https://not-an-instagram-url.com>
Expand Down
6 changes: 4 additions & 2 deletions src/transformers/Instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export const shouldTransform = (url) => {
);
};

export const getHTML = (url) =>
export const getHTML = (url, { accessToken }) =>
fetchOEmbedData(
`https://api.instagram.com/oembed?url=${url}&omitscript=true`
`https://graph.facebook.com/v8.0/instagram_oembed?url=${url}&access_token=${accessToken}&omitscript=true`
).then(({ html }) => html);

export const name = 'Instagram';