Phase 6 part 4: post as the LinkedIn company page
A second LinkedIn app (Community Management API) with its own sign-in on Settings → Social accounts: the pages the member may post for are listed and one chosen, new LinkedIn drafts default to the page or to you, and each draft can switch before it's published. Schema 25. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -180,6 +180,10 @@ var accountFields = []secretField{
|
||||
"Create an app (it needs a LinkedIn company page), add the products Share on LinkedIn and Sign In with LinkedIn using OpenID Connect, then open the Auth tab.", linkedInApps},
|
||||
{"LinkedIn", secrets.LinkedInClientSecret, "Client secret",
|
||||
"On the same Auth tab, under the client ID.", linkedInApps},
|
||||
{"LinkedIn page", secrets.LinkedInPageClientID, "Client ID",
|
||||
"A second LinkedIn app, linked to the company page, with the Community Management API approved by LinkedIn (it must be the app's only product). Its Auth tab has the ID.", linkedInApps},
|
||||
{"LinkedIn page", secrets.LinkedInPageClientSecret, "Client secret",
|
||||
"On the same Auth tab, under the client ID.", linkedInApps},
|
||||
{"Reddit", secrets.RedditClientID, "Client ID",
|
||||
"Create app → choose \"web app\" and enter the redirect URI below. The client ID is the string under the app's name.", redditApps},
|
||||
{"Reddit", secrets.RedditClientSecret, "Client secret",
|
||||
@@ -228,7 +232,8 @@ type connectInfo struct {
|
||||
Account string
|
||||
At time.Time
|
||||
Note string
|
||||
Expiry *Expiry // when the sign-in runs out, for LinkedIn
|
||||
Expiry *Expiry // when the sign-in runs out, for LinkedIn
|
||||
Page *pageSetup // the company page's choices, for the LinkedIn page
|
||||
}
|
||||
|
||||
func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -239,7 +244,7 @@ func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// networks are the cards on the Social accounts tab, as things stand.
|
||||
func (s *Server) networks() []network {
|
||||
names := []string{secrets.XAccessToken, secrets.LinkedInAccessToken, secrets.RedditAccessToken}
|
||||
names := []string{secrets.XAccessToken, secrets.LinkedInAccessToken, secrets.LinkedInPageAccessToken, secrets.RedditAccessToken}
|
||||
for _, f := range accountFields {
|
||||
names = append(names, f.Name)
|
||||
}
|
||||
@@ -259,7 +264,7 @@ func (s *Server) networks() []network {
|
||||
}
|
||||
}
|
||||
var nets []network
|
||||
for _, name := range []string{"X", "LinkedIn", "Reddit", "YouTube"} {
|
||||
for _, name := range []string{"X", "LinkedIn", liPageNetwork, "Reddit", "YouTube"} {
|
||||
n := network{Name: name, Set: set}
|
||||
for _, f := range accountFields {
|
||||
if f.Group == name {
|
||||
@@ -274,6 +279,12 @@ func (s *Server) networks() []network {
|
||||
n.Connect = connect("linkedin", secrets.LinkedInAccessToken, "In your LinkedIn app's Auth settings, add this redirect URL:", "",
|
||||
set[secrets.LinkedInClientID] && set[secrets.LinkedInClientSecret], func(string) string { return "" })
|
||||
n.Connect.Expiry = s.linkedInExpiry()
|
||||
case liPageNetwork:
|
||||
n.Connect = connect(liPageProvider, secrets.LinkedInPageAccessToken, "In the page app's Auth settings, add this redirect URL:",
|
||||
"Posts can then go out as the company page instead of you; each LinkedIn draft says which.",
|
||||
set[secrets.LinkedInPageClientID] && set[secrets.LinkedInPageClientSecret], func(a string) string { return a })
|
||||
n.Connect.Expiry = s.linkedInPageExpiry()
|
||||
n.Connect.Page = s.pageSetup()
|
||||
case "Reddit":
|
||||
n.Connect = connect("reddit", secrets.RedditAccessToken, "At reddit.com/prefs/apps, create a web app with this redirect URI:",
|
||||
"Reddit's API terms need approval for commercial use: Supermarket is meant for a few hand-approved posts, not volume.",
|
||||
@@ -351,6 +362,13 @@ func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
authURL, p, err = oauth.LinkedInStart(id, s.callbackURL("linkedin"))
|
||||
case liPageProvider:
|
||||
id := s.Secrets.Get(secrets.LinkedInPageClientID)
|
||||
if id == "" || !s.Secrets.Has(secrets.LinkedInPageClientSecret) {
|
||||
http.Error(w, "Save the LinkedIn page app's client ID and secret first.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
authURL, p, err = oauth.LinkedInStartScopes(liPageProvider, id, s.callbackURL(liPageProvider), s.liPageScopes())
|
||||
case "reddit":
|
||||
id := s.Secrets.Get(secrets.RedditClientID)
|
||||
if id == "" || !s.Secrets.Has(secrets.RedditClientSecret) {
|
||||
@@ -489,6 +507,33 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
|
||||
s.Log.Error("saving connection", "err", err)
|
||||
}
|
||||
redirect(w, r, "/settings/accounts", "li-connected")
|
||||
case liPageProvider:
|
||||
res, err := oauth.LinkedInExchange(ctx, s.Secrets.Get(secrets.LinkedInPageClientID), s.Secrets.Get(secrets.LinkedInPageClientSecret), code, p)
|
||||
if err != nil {
|
||||
fail(err.Error())
|
||||
return
|
||||
}
|
||||
if err := s.Secrets.Set(map[string]string{secrets.LinkedInPageAccessToken: res.AccessToken}); err != nil {
|
||||
fail("Signed in, but couldn't save the token: " + err.Error())
|
||||
return
|
||||
}
|
||||
life := res.ExpiresIn
|
||||
if life <= 0 {
|
||||
life = linkedInTokenLife
|
||||
}
|
||||
now := time.Now()
|
||||
_, listErr := s.pageOrgs(ctx, res.AccessToken)
|
||||
if listErr != nil {
|
||||
s.Log.Warn("linkedin page: couldn't list the pages", "err", listErr)
|
||||
}
|
||||
account := ""
|
||||
if o := s.liPage(); o != nil {
|
||||
account = o.Name
|
||||
}
|
||||
if err := s.saveConnection(liPageProvider, Connection{Account: account, At: now, Expires: now.Add(life)}); err != nil {
|
||||
s.Log.Error("saving connection", "err", err)
|
||||
}
|
||||
redirect(w, r, "/settings/accounts", "li-page-connected")
|
||||
case "reddit":
|
||||
res, err := oauth.RedditExchange(ctx, s.Secrets.Get(secrets.RedditClientID), s.Secrets.Get(secrets.RedditClientSecret),
|
||||
s.redditUserAgent(), code, p)
|
||||
|
||||
Reference in New Issue
Block a user