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:
@@ -198,3 +198,115 @@ func fallbackTitle(u string) string {
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// --- Company pages (the Community Management API) ---
|
||||
|
||||
// Org is a LinkedIn company page the signed-in member may post for.
|
||||
type Org struct {
|
||||
URN string `json:"urn"` // urn:li:organization:<id>
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Vanity string `json:"vanity"` // linkedin.com/company/<vanity>
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
// postingRoles may post on a page's behalf (w_organization_social). The
|
||||
// docs name the content role both ways.
|
||||
var postingRoles = map[string]bool{"ADMINISTRATOR": true, "CONTENT_ADMIN": true, "CONTENT_ADMINISTRATOR": true,
|
||||
"DIRECT_SPONSORED_CONTENT_POSTER": true}
|
||||
|
||||
// get sends a versioned REST GET, stepping back a version on 426 as
|
||||
// PostUpdate does, and decodes the JSON answer.
|
||||
func (c *Client) get(ctx context.Context, path string, out any) error {
|
||||
var (
|
||||
res *http.Response
|
||||
raw []byte
|
||||
)
|
||||
for attempt := 0; ; attempt++ {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiBase+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.accessToken)
|
||||
req.Header.Set("LinkedIn-Version", version(c.now(), versionLag+attempt))
|
||||
req.Header.Set("X-Restli-Protocol-Version", "2.0.0")
|
||||
res, err = c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, _ = io.ReadAll(io.LimitReader(res.Body, 4<<20))
|
||||
res.Body.Close()
|
||||
if res.StatusCode == http.StatusUpgradeRequired && attempt+1 < versionAttempts {
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
switch {
|
||||
case res.StatusCode == http.StatusUnauthorized:
|
||||
return ErrUnauthorized
|
||||
case res.StatusCode == http.StatusForbidden:
|
||||
return fmt.Errorf("LinkedIn refused (403): the app may lack the permission for this (%s)", strings.TrimSpace(string(raw[:min(len(raw), 300)])))
|
||||
case res.StatusCode != http.StatusOK:
|
||||
return fmt.Errorf("linkedin HTTP %d: %s", res.StatusCode, raw[:min(len(raw), 300)])
|
||||
}
|
||||
return json.Unmarshal(raw, out)
|
||||
}
|
||||
|
||||
// Organizations lists the company pages the member has an approved role on
|
||||
// that may post (administrator, content admin, sponsored content poster),
|
||||
// with their names.
|
||||
func (c *Client) Organizations(ctx context.Context) ([]Org, error) {
|
||||
var acls struct {
|
||||
Elements []struct {
|
||||
Role string `json:"role"`
|
||||
State string `json:"state"`
|
||||
Organization string `json:"organization"`
|
||||
OrganizationTarget string `json:"organizationTarget"`
|
||||
} `json:"elements"`
|
||||
}
|
||||
if err := c.get(ctx, "/rest/organizationAcls?q=roleAssignee&state=APPROVED&count=100", &acls); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var orgs []Org
|
||||
seen := map[string]bool{}
|
||||
for _, e := range acls.Elements {
|
||||
urn := e.OrganizationTarget
|
||||
if urn == "" {
|
||||
urn = e.Organization
|
||||
}
|
||||
id := strings.TrimPrefix(urn, "urn:li:organization:")
|
||||
if id == urn || id == "" || e.State != "APPROVED" || !postingRoles[e.Role] || seen[id] {
|
||||
continue
|
||||
}
|
||||
seen[id] = true
|
||||
orgs = append(orgs, Org{URN: urn, ID: id, Role: e.Role})
|
||||
}
|
||||
if len(orgs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ids := make([]string, len(orgs))
|
||||
for i, o := range orgs {
|
||||
ids[i] = o.ID
|
||||
}
|
||||
var look struct {
|
||||
Results map[string]struct {
|
||||
LocalizedName string `json:"localizedName"`
|
||||
VanityName string `json:"vanityName"`
|
||||
} `json:"results"`
|
||||
}
|
||||
// organizationsLookup doesn't need the administrator role; a page
|
||||
// whose name can't be read is still listed, by its ID.
|
||||
if err := c.get(ctx, "/rest/organizationsLookup?ids=List("+strings.Join(ids, ",")+")", &look); err == nil {
|
||||
for i := range orgs {
|
||||
if r, ok := look.Results[orgs[i].ID]; ok {
|
||||
orgs[i].Name, orgs[i].Vanity = r.LocalizedName, r.VanityName
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range orgs {
|
||||
if orgs[i].Name == "" {
|
||||
orgs[i].Name = "Page " + orgs[i].ID
|
||||
}
|
||||
}
|
||||
return orgs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user