1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package linkedin
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"codeberg.org/snonux/gos/gosdir/db/platforms/linkedin/oauth2"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
)
// TODO: Also implemebt a Text Platform output, which then laster can be
// processed by Gemtexter as a page
func Post(ctx context.Context, args config.Args, ent entry.Entry) error {
secrets, err := oauth2.AccessToken(args)
if err != err {
return err
}
// TODO: Don't log this anymore
log.Println("DEBUG", "Got access token", secrets)
return nil
}
func postMessage(secrets config.Secrets, message string) error {
const url = "https://api.linkedin.com/v2/posts"
post := map[string]interface{}{
"author": fmt.Sprintf("urn:li:person:%s", secrets.LinkedInPesonID),
"commentary": message,
"visibility": "PUBLIC",
"distribution": map[string]interface{}{
"feedDistribution": "MAIN_FEED",
"targetEntities": []string{},
"thirdPartyDistributionChannels": []string{},
},
"lifecycleState": "PUBLISHED",
"isReshareDisabledByAuthor": false,
}
payload, err := json.Marshal(post)
if err != nil {
return fmt.Errorf("Error encoding JSON:%w", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("Error creating request: %w", err)
}
req.Header.Add("Authorization", "Bearer "+secrets.LinkedInAccessToken)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("X-RestLi-Protocol-Version", "2.0.0")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error sending request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("Failed to post to LinkedIn. Status: %s\n%s\n\n", resp.Status, body)
}
return nil
}
|