Files
social-updater/lib/linkedin.go
2026-05-12 16:42:53 +02:00

71 lines
1.4 KiB
Go

package lib
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type Linkedin struct {
accessToken string
authorURN string
client *http.Client
}
func NewLinkedin(accessToken, authorURN string) *Linkedin {
return &Linkedin{
accessToken: accessToken,
authorURN: authorURN,
client: &http.Client{},
}
}
func (l *Linkedin) Post(video Video) error {
payload := map[string]any{
"author": l.authorURN,
"lifecycleState": "PUBLISHED",
"specificContent": map[string]any{
"com.linkedin.ugc.ShareContent": map[string]any{
"shareCommentary": map[string]any{
"text": video.Title,
},
"shareMediaCategory": "ARTICLE",
"media": []map[string]any{
{
"status": "READY",
"originalUrl": video.URL,
},
},
},
},
"visibility": map[string]any{
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC",
},
}
body, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, "https://api.linkedin.com/v2/ugcPosts", bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+l.accessToken)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Restli-Protocol-Version", "2.0.0")
resp, err := l.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("linkedin api error: status %d", resp.StatusCode)
}
return nil
}