Move to Federated Gitea
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package linkedin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
apiBase = "https://api.linkedin.com"
|
||||
linkedinVersion = "202501"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
accessToken string
|
||||
authorURN string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(accessToken, authorURN string) *Client {
|
||||
return &Client{
|
||||
accessToken: accessToken,
|
||||
authorURN: authorURN,
|
||||
http: &http.Client{Timeout: 15 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
// PostUpdate creates a LinkedIn post. If sourceURL is non-empty it is attached
|
||||
// as an article card (LinkedIn will fetch the title and thumbnail automatically).
|
||||
func (c *Client) PostUpdate(ctx context.Context, text, sourceURL string) (string, error) {
|
||||
pr := postRequest{
|
||||
Author: c.authorURN,
|
||||
Commentary: text,
|
||||
Visibility: "PUBLIC",
|
||||
Distribution: distribution{
|
||||
FeedDistribution: "MAIN_FEED",
|
||||
TargetEntities: []any{},
|
||||
ThirdPartyDistributionChannels: []any{},
|
||||
},
|
||||
LifecycleState: "PUBLISHED",
|
||||
IsReshareDisabledByAuthor: false,
|
||||
}
|
||||
if sourceURL != "" {
|
||||
pr.Content = &postContent{Article: articleContent{Source: sourceURL}}
|
||||
}
|
||||
|
||||
body, err := json.Marshal(pr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiBase+"/rest/posts", strings.NewReader(string(body)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.accessToken)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("LinkedIn-Version", linkedinVersion)
|
||||
req.Header.Set("X-Restli-Protocol-Version", "2.0.0")
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
raw, _ := io.ReadAll(res.Body)
|
||||
if res.StatusCode != http.StatusCreated {
|
||||
return "", fmt.Errorf("linkedin HTTP %d: %s", res.StatusCode, raw)
|
||||
}
|
||||
|
||||
postID := res.Header.Get("X-RestLi-Id")
|
||||
if postID == "" {
|
||||
return "", nil
|
||||
}
|
||||
return "https://www.linkedin.com/feed/update/" + postID + "/", nil
|
||||
}
|
||||
|
||||
// GetAuthorURN fetches the LinkedIn person URN for the owner of accessToken.
|
||||
func GetAuthorURN(ctx context.Context, accessToken string) (string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiBase+"/v2/userinfo", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
raw, _ := io.ReadAll(res.Body)
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("userinfo HTTP %d: %s", res.StatusCode, raw)
|
||||
}
|
||||
|
||||
var info struct {
|
||||
Sub string `json:"sub"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &info); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if info.Sub == "" {
|
||||
return "", fmt.Errorf("no sub in userinfo response: %s", raw)
|
||||
}
|
||||
return "urn:li:person:" + info.Sub, nil
|
||||
}
|
||||
|
||||
type postRequest struct {
|
||||
Author string `json:"author"`
|
||||
Commentary string `json:"commentary"`
|
||||
Visibility string `json:"visibility"`
|
||||
Distribution distribution `json:"distribution"`
|
||||
Content *postContent `json:"content,omitempty"`
|
||||
LifecycleState string `json:"lifecycleState"`
|
||||
IsReshareDisabledByAuthor bool `json:"isReshareDisabledByAuthor"`
|
||||
}
|
||||
|
||||
type distribution struct {
|
||||
FeedDistribution string `json:"feedDistribution"`
|
||||
TargetEntities []any `json:"targetEntities"`
|
||||
ThirdPartyDistributionChannels []any `json:"thirdPartyDistributionChannels"`
|
||||
}
|
||||
|
||||
type postContent struct {
|
||||
Article articleContent `json:"article"`
|
||||
}
|
||||
|
||||
type articleContent struct {
|
||||
Source string `json:"source"`
|
||||
}
|
||||
Reference in New Issue
Block a user