initial commit
This commit is contained in:
70
lib/linkedin.go
Normal file
70
lib/linkedin.go
Normal file
@@ -0,0 +1,70 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user