ReviewOS

also looking at this

stacks/postline

feat: crossposting — media (LinkedIn/IG/Threads), OAuth token refresh, and X/Twitter

#30
Merged glennmichael123 wants to merge feat/crosspost-media-and-token-refresh into main
28 files +1810 -109
app/Services/Social/Drivers/InstagramDriver.tsmodified+42-1
Changes to app/Services/Social/Drivers/InstagramDriver.ts
@@ -1,1 +1,42 @@
1export { InstagramApiError, InstagramPublishingDriver as InstagramDriver } from '@stacksjs/socials'
1import { InstagramApiError, InstagramPublishingDriver } from '@stacksjs/socials'
2
3export { InstagramApiError }
4
5export interface InstagramLongLivedToken {
6 accessToken: string
7 expiresIn?: number
8}
9
10/**
11 * App-owned Instagram driver. Extends the vendored `@stacksjs/socials` driver
12 * with the Facebook long-lived-token exchange. Kept in `app/` so a framework
13 * upgrade can't clobber it; publishing and account resolution are inherited.
14 *
15 * Instagram publishes with a Page access token derived from a user token. A
16 * Page token minted from a *long-lived* user token does not expire on its own,
17 * so the "keep the connection alive" step for Instagram is exchanging the
18 * short-lived user token for a long-lived one before resolving the account —
19 * there is no per-publish refresh grant the way Threads/LinkedIn have.
20 */
21export class InstagramDriver extends InstagramPublishingDriver {
22 /** Exchange a short-lived user token for a long-lived (~60-day) one. */
23 async exchangeLongLivedUserToken(clientId: string, clientSecret: string, shortLivedToken: string): Promise<InstagramLongLivedToken> {
24 const query = new URLSearchParams({
25 grant_type: 'fb_exchange_token',
26 client_id: clientId,
27 client_secret: clientSecret,
28 fb_exchange_token: shortLivedToken,
29 })
30
31 const payload = await this.graph<{ access_token?: string, expires_in?: number }>(
32 `/oauth/access_token?${query.toString()}`,
33 { method: 'GET' },
34 )
35
36 if (!payload.access_token) {
37 throw new InstagramApiError('Facebook did not return a long-lived token.', 400, JSON.stringify(payload))
38 }
39
40 return { accessToken: payload.access_token, expiresIn: payload.expires_in }
41 }
42}