ADR: LinkedIn posting through the self-serve API
Status: accepted, 2026-11-29, at 1.0.5.197. The tables blog.LinkedInConnection, blog.LinkedInState and blog.LinkedInPost, the routes under /api/linkedin/ and /api/admin/blog/posts/{slug}/linkedin, and the two optional app settings ship at 1.0.5.197, reading "Not set up yet" until the LinkedIn app's two values exist.
Reading list
- ADR: Every LinkedIn post is also a blog post: the blog post each LinkedIn post is the twin of, and its one LinkedIn address.
- ADR: Data first, and the database in source control: who writes the schema and who publishes it.
- ADR: Automated tests never touch a real database or permanent storage: why every LinkedIn call in the suite is a script.
- LinkedIn's own pages, read on learn.microsoft.com/linkedin at version li-lms-2026-11 on Sunday 29 November: Authorization Code Flow (the 60-day token, and going through the authorize address again while signed in skips the Allow screen), Programmatic Refresh Tokens (for a limited set of partners only), Posts API, Documents API, Comments API, Little Text Format, and the versioning page.
In plain words
Posts went to LinkedIn by driving LinkedIn's composer in a browser: a patched file input, a title field that dropped letters, duplicate comments, overlays over the Comment button. Every one of those was a place the posting lane broke. LinkedIn's self-serve "Share on LinkedIn" product lets an app post on the member's own profile, with a carousel, through an API.
The catch is the 60 days. LinkedIn gives a self-serve app a token that lasts 60 days and no way to refresh it. What makes that livable is a second fact on the same page: while the member is signed in to LinkedIn and the token has not expired, going through the authorize address again skips the Allow screen. So renewing before day 60 is one tap on one link. The card counts the days, the morning card says so from day 50, and a calendar reminder carries the link.
Nothing goes dark when anything fails: the browser route stays as the floor, and every refusal says in plain words that the post goes by the browser.
Context
- A blog post already keeps one LinkedInUrl, unique, with a
?pending=<slug> placeholder until the post is live.
- The site's secrets reach the App Service at roll time from GitHub repository secrets (deploy.yml, the Roll the site step). No Key Vault is read anywhere.
- A self-serve app holds
openid profile w_member_social. The Comments API's permission table names w_member_social_feed for a member's comment, which a self-serve app does not get; that the versioned /rest routes take a self-serve token is listed by the Posts and Documents pages and is proven for this app only by the first live calls.
Decision
- The scopes are exactly
openid profile w_member_social, in one constant: asking for a different scope invalidates every earlier token.
- The app's two values are GitHub repository secrets LINKEDIN_CLIENT_ID and LINKEDIN_CLIENT_SECRET, filled into
LinkedIn__ClientId and LinkedIn__ClientSecret at roll time as optional settings: a roll before they exist neither warns nor fails. Unset, empty or starting with two underscores reads as not set up, the operator key's rule. Not Key Vault: the site reads none, and a vault costs a resource for two values.
- The 60-day token is sealed in the database, one row in
blog.LinkedInConnection: AES-GCM with a fresh 12-byte nonce every time, under a key that is HKDF-SHA256 over the session signing key with the info string theyard.linkedin.token.v1. Its expiry is LinkedIn's expires_in, never a constant. It is never logged, never answered by a route, never written to a file. A row that will not open (the signing key changed) reads as lapsed: one reconnect.
- The keyed connect pins the account. The card's Connect LinkedIn button, with the operator's key, keeps the member id userinfo answers as the owner, and sets a ten-minute
__Host- cookie the callback must see. The key can always re-pin, because a new LinkedIn app means a new member id.
- The public reconnect link renews only the pinned account. It needs no key and no cookie, so it works from a phone's mail or calendar; any other account gets "That was a different LinkedIn account" and nothing is kept. Before anybody is pinned it goes straight back. It is limited to ten a minute per visitor, and no page shows it without the key.
- The state is 32 random bytes, kept as its SHA-256 for ten minutes and used once. The callback never draws a page of its own: it redirects to the Admin card with
linkedin=ok|wrong_account|error and a reason code, and the page maps the code to its own sentence. The connect routes and the reminder answer no-store.
- The connect checks the carousel once. After a token is kept, one upload-slot call for the owner (nothing uploaded) says whether the Documents API will take this app's carousel, so the card answers that before any post.
- Posting is on the operator's yes only, and what goes out is what he was shown. A chat saves the feed text, first comment, document title and carousel PDF, shows the dry run, and on "Post that" calls publish with the dry run's hash, SHA-256 over the escaped commentary, the comment, the title and the PDF's bytes. Nothing calls publish on a timer.
- Never twice. Each publish step is recorded on the row before the next: the document, the post, the blog post's LinkedIn address (through
IBlogStore.SetLinkedInAsync, which sets two fields and nothing else, never the save that replaces a post whole), the first comment. A row with a post urn is never posted again and a retry carries on from the first step not recorded. A blog post whose LinkedInUrl is already a live post's address went out by the browser and is refused. A call whose answer never came back is unknown, not failed, and holds the post until a chat has read the live post and settled it.
- The first comment has two fallbacks and a floor. The versioned route first, then the older
/v2/socialActions route once; if LinkedIn refuses both, the post still counts as live, the comment goes by the browser, and that route is remembered so the dry run says so up front.
- The commentary is Little Text Format, escaped once in one pure function: every reserved character gets a backslash, a hashtag becomes
{hashtag|\#|Word}, and line breaks, emoji and Unicode bold pass through. A post that tags a person is refused for the browser, because the self-serve API cannot look a member up.
- The version header is one constant,
202611. A LinkedIn version lives about a year (2026-01 retires on 15 January 2027), so it is bumped before November 2027; a call LinkedIn answers as a retired version refuses with version_retired.
The 60 days, and who acts on them
day 0 keyed connect (the card's button) -> owner pinned, token sealed, carousel checked
day 0-49 card: "Connected. N days left." -> "Add a reminder to my calendar" (.ics for day 50)
day 50 card amber, morning card line -> one tap on the reconnect link: Allow is skipped
day 60 card red, morning card NOT WORKING -> posts go by the browser; the link plus Allow renews
Consequences
- Steve's 15-minute hookup on Monday (the LinkedIn app, its two values as repository secrets, one roll, the Connect LinkedIn button) is the first live call to LinkedIn; until then the card and the status say "Not set up yet".
- The comment may never go by the API for a self-serve app; the design treats that as the expected case, not a failure.
- Rotating the session signing key costs one reconnect.
- The schema is published by the lane before the roll, with sqlpackage's deploy report first and
BlockOnPossibleDataLoss=true; until it lands the status reads not set up and the writes answer 503.
Where it sits
In the Blog slice of the onion, because it reads and writes blog posts and a slice may not reach another: its files sit beside the blog's, named LinkedIn, in Domain, Application and Infrastructure, and in the host it is its own folder Features/Blog/LinkedIn with its own Module. Domain holds the pure rules (LinkedInText, LinkedInClock, LinkedInRefusals, LinkedInPostBody, LinkedInReminder); Application holds the ports (ILinkedInReader, ILinkedInStore, ILinkedInClient), the seal and one handler per request; Infrastructure holds the relational store (EfLinkedInStore) and the one HTTP adapter (HttpLinkedInClient) on the shared pool; the host holds the routes, the limiter policy and the wiring. It serves dependency inversion (LinkedIn is a port, a script in every test) and keeps the browser route as the floor. It costs three small tables and no new Azure resource.
Enforced by
Not machine-checked. Held by review.