# Social Platform Connection Guide

CoreFlow talks to each platform's **official API only**. To connect an account you need an app of your own on that platform's developer console, its client ID/secret pasted into CoreFlow, the correct redirect URL registered, and the permissions below requested.

## How connection works

1. **Create the app** in the platform's developer console (links per platform below).
2. **Register the redirect URL** in the app exactly as CoreFlow shows it under *Social Settings → API credentials → OAuth redirect URLs*:
   `https://<your-domain>/social/oauth/<platform>/callback`
   The base comes from `APP_URL` in `erp-core-backend/.env`, so set that to your public HTTPS domain before connecting.
3. **Paste the client ID and secret** into *Social Settings → API credentials* (stored server-side, never shown again). Values can also come from `.env` (`META_APP_ID`, `LINKEDIN_CLIENT_ID`, …).
4. **Connect** from *Social → Accounts → Connect*. You are sent to the platform's login/consent screen; the returned account is stored with an encrypted token.
5. **Webhooks (optional, real-time)** for Facebook, Instagram and X are registered on the same console. CoreFlow exposes:
   `https://<your-domain>/api/social/webhooks/<platform>`
   Facebook/Instagram use the *Meta verify token* from Social Settings; X uses a CRC handshake signed with its client secret.

Tokens are encrypted at rest. Facebook/Instagram/Threads use long-lived Meta tokens; Threads, Pinterest, Reddit and X can be refreshed from *Social → Accounts → Reconnect*.

---

## Facebook (Pages)

- **Console:** https://developers.facebook.com/apps
- **Redirect:** `/social/oauth/facebook/callback`
- **Permissions:** `pages_show_list`, `pages_read_engagement`, `pages_manage_posts`, `pages_read_user_content`, `pages_messaging`, `read_insights`
- **Steps:** create a Business app → add the *Facebook Login* product → add your Page → request the permissions above → submit the app for review (`pages_manage_posts` and `pages_messaging` are advanced).
- **Can do:** publish to the Page feed and photos, reply to comments, send/receive Messenger DMs, read post insights.
- **Webhook:** object `page`, subscribe `feed` and `messages`.

## Instagram (Business/Creator via Facebook Login)

- **Console:** same Meta app as Facebook.
- **Redirect:** `/social/oauth/instagram/callback`
- **Permissions:** `instagram_basic`, `instagram_content_publish`, `instagram_manage_comments`, `instagram_manage_messages`, `instagram_manage_insights`, `pages_show_list`, `pages_manage_metadata`, `business_management`
- **Steps:** the Instagram account must be a **Business/Creator account linked to a Facebook Page**. CoreFlow lists the linked IG account through `instagram_business_account`.
- **Can do:** publish image/reel containers, reply to comments, send/receive Instagram DMs, read media insights.
- **Webhook:** object `instagram`, subscribe `comments`, `mentions`, `messages`.

## Threads

- **Console:** same Meta app; add the **Threads** use case and copy the *Threads App ID/secret*.
- **Redirect:** `/social/oauth/threads/callback`
- **Permissions:** `threads_basic`, `threads_content_publish`, `threads_read_replies`, `threads_manage_replies`, `threads_manage_insights`
- **Steps:** enrollment is required for the Threads API. Authorization uses `threads.com`, token exchange `graph.threads.com`, data calls `graph.threads.net/v1.0`. Tokens are upgraded to long-lived automatically.
- **Can do:** publish text/image posts and replies, read replies and insights. **No webhooks** — replies arrive on the 5-minute poll.

## LinkedIn

- **Console:** https://www.linkedin.com/developers/apps
- **Redirect:** `/social/oauth/linkedin/callback`
- **Permissions:** `openid`, `profile`, `w_member_social`, `w_organization_social`, `r_organization_social`, `w_organization_social_feed`, `r_organization_social_feed`, `rw_organization_admin`
- **Steps:** create an app, associate it with your Company Page, then request the **Share on LinkedIn** product (`w_member_social`) and the **Community Management API** product (organization scopes). Org scopes need partner-style approval.
- **Can do:** publish text and image posts to a profile or Company Page, read/reply to comments, read social metadata. Uses the versioned `/rest` APIs; the API version is `LINKEDIN_API_VERSION` (default `202601`).
- **Note:** image posts upload through the Images API (`POST /rest/images?action=registerUpload`, then a presigned `PUT` to the returned `uploadUrl`, then the asset URN in `content.media`). Document posts are not wired yet.

## Pinterest

- **Console:** https://developers.pinterest.com/apps/
- **Redirect:** `/social/oauth/pinterest/callback`
- **Permissions:** `boards:read`, `pins:read`, `pins:write`, `user_accounts:read`, `catalogs:read`
- **Steps:** create an app, request write scopes, add the redirect URL. Standard/ trial access is enough for pins; analytics needs the account to meet Pinterest's thresholds.
- **Can do:** create Pins on a board, read profile stats, read Pin analytics. **The v5 API has no organic comment endpoints**, so comments and DMs are not available.

## Reddit

- **Console:** https://www.reddit.com/prefs/apps
- **Redirect:** `/social/oauth/reddit/callback`
- **Permissions:** `identity`, `submit`, `read`, `modposts`, `privatemessages`
- **Steps:** create a **web app**, set the redirect URL, copy the client ID/secret. Reddit requires a unique `User-Agent` (CoreFlow sends `CoreFlowERP/1.0`).
- **Can do:** submit self-posts to a subreddit, read and reply to comments, read the unread inbox. DM sending is not wired (Reddit DMs use `/api/compose`).

## X

- **Console:** https://developer.x.com/en/portal/dashboard
- **Redirect:** `/social/oauth/x/callback`
- **Permissions:** `tweet.read`, `tweet.write`, `users.read`, `like.read`, `media.write`, `offline.access`
- **Steps:** create an app, enable **OAuth 2.0 with PKCE** (CoreFlow generates the challenge), add the redirect URL, and set the app to *Read and write*. Media upload uses the v2 media endpoint.
- **Can do:** publish posts with one image, reply to mentions/tweets, read metrics. **No DM sending** (`dm.write` is not implemented). Webhook (Account Activity API) delivers mentions and DMs in real time.

## Google News

- **Console:** https://publishercenter.google.com/
- **No OAuth and no publishing API.** Google News has no public content API — publication is discovered from the RSS/Atom feed or news sitemap you host on your own site.
- **Steps:** connect from *Social → Accounts → Connect publisher*, paste your feed URL. CoreFlow parses the feed for headlines, sends article links to marketing posts, and uses it for monitoring.
- **Can do:** feed ingestion and headlines only.

---

## Capability matrix

| Platform | Publish | Comments & replies | Direct messages | Inbound inbox | Analytics |
| --- | --- | --- | --- | --- | --- |
| Facebook | Yes | Yes | Yes | Yes (webhook) | Yes |
| Instagram | Yes | Yes | Yes | Yes (webhook) | Yes |
| Threads | Yes | Yes | No | Yes (poll) | Yes |
| LinkedIn | Yes | Yes | No | Yes (poll) | Yes |
| Pinterest | Yes | No | No | No | Yes |
| Reddit | Yes | Yes | No | Yes (poll) | Yes |
| X | Yes | Yes | No | Yes (webhook) | Yes |
| Google News | No | No | No | No | No |

This table is also rendered live under *Social Settings & Security → Security → Platform capabilities*.

## Settings / environment reference

| Setting key | Purpose |
| --- | --- |
| `social_meta_app_id` / `social_meta_app_secret` | Meta app used by Facebook, Instagram and Threads |
| `social_meta_verify_token` | Webhook verification token for Facebook/Instagram |
| `social_linkedin_app_id` / `_secret` | LinkedIn app credentials |
| `social_pinterest_app_id` / `_secret` | Pinterest app credentials |
| `social_reddit_app_id` / `_secret` | Reddit app credentials |
| `social_x_app_id` / `_secret` | X app credentials |
| `APP_URL` | Base for every OAuth redirect URI and webhook URL |
| `LINKEDIN_API_VERSION` | LinkedIn versioned API header (default `202601`) |
| `THREADS_GRAPH_VERSION` | Threads API version (default `v1.0`) |
| `META_GRAPH_VERSION` | Facebook/Instagram Graph version (default `v23.0`) |
