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/ThreadsDriver.tsmodified+66-1
Changes to app/Services/Social/Drivers/ThreadsDriver.ts
@@ -1,1 +1,66 @@
1export { ThreadsApiError, ThreadsPublishingDriver as ThreadsDriver } from '@stacksjs/socials'
1import { ThreadsApiError, ThreadsPublishingDriver } from '@stacksjs/socials'
2
3export { ThreadsApiError }
4
5export interface ThreadsLongLivedToken {
6 accessToken: string
7 expiresIn?: number
8}
9
10/**
11 * App-owned Threads driver. Extends the vendored `@stacksjs/socials` driver with
12 * long-lived-token exchange and refresh — the base driver's `exchangeCode` only
13 * mints a short-lived (~1h) token. Kept in `app/` so a framework upgrade can't
14 * clobber it; publishing and timeline are inherited unchanged.
15 *
16 * Both token endpoints are unversioned (no `/vX` prefix), so they can't go
17 * through the base `graph()` helper — they use a small local `tokenRequest`.
18 */
19export class ThreadsDriver extends ThreadsPublishingDriver {
20 /**
21 * Exchange a short-lived token for a long-lived (~60-day) one. Requires the
22 * app's client secret. Called once right after the OAuth code exchange.
23 */
24 async exchangeLongLivedToken(clientSecret: string, shortLivedToken: string): Promise<ThreadsLongLivedToken> {
25 const query = new URLSearchParams({
26 grant_type: 'th_exchange_token',
27 client_secret: clientSecret,
28 access_token: shortLivedToken,
29 })
30 const payload = await this.tokenRequest(`${this.graphBase}/access_token?${query.toString()}`)
31 return { accessToken: payload.access_token, expiresIn: payload.expires_in }
32 }
33
34 /**
35 * Refresh an unexpired long-lived token, extending it another ~60 days.
36 * Threads requires the token be at least 24h old and not yet expired — both
37 * hold when the proactive refresh fires inside the pre-expiry window.
38 */
39 async refreshLongLivedToken(accessToken: string): Promise<ThreadsLongLivedToken> {
40 const query = new URLSearchParams({
41 grant_type: 'th_refresh_token',
42 access_token: accessToken,
43 })
44 const payload = await this.tokenRequest(`${this.graphBase}/refresh_access_token?${query.toString()}`)
45 return { accessToken: payload.access_token, expiresIn: payload.expires_in }
46 }
47
48 private async tokenRequest(url: string): Promise<{ access_token: string, expires_in?: number }> {
49 const response = await fetch(url, { method: 'GET' })
50 const text = await response.text()
51 let json: any = {}
52 try {
53 json = text ? JSON.parse(text) : {}
54 }
55 catch {
56 json = {}
57 }
58
59 if (!response.ok || json?.error || !json?.access_token) {
60 const message = json?.error?.message || json?.error_message || text || response.statusText
61 throw new ThreadsApiError(`Threads token request failed (${response.status}): ${message}`, response.status, text)
62 }
63
64 return json
65 }
66}