-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
115 lines (94 loc) · 2.66 KB
/
share.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package golinkedin
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type Post struct {
// Author URN for this content
Author string `json:"author"`
// The state of this content. PUBLISHED is the only accepted field during creation.
LifeCycleState string `json:"lifecycleState"`
// The content of post. For now you can just define text.
SpecificContent SpecificContent `json:"specificContent"`
// Visibility restrictions on content.
Visibility Visibility `json:"visibility"`
}
type SpecificContent struct {
ShareContent ShareContent `json:"com.linkedin.ugc.ShareContent"`
}
type ShareContent struct {
ShareCommentary ShareCommentary `json:"shareCommentary"`
ShareMediaCategory string `json:"shareMediaCategory"`
}
type ShareCommentary struct {
Text string `json:"text"`
}
type Visibility struct {
Code string `json:"com.linkedin.ugc.MemberNetworkVisibility"`
}
// JOB POSTING STRUCTS
type JobValue struct {
JobPosting []JobPosting `json:"elements"`
}
type JobPosting struct {
IntegrationContext string `json:"integrationContext"`
CompanyApplyUrl string `json:"companyApplyUrl"`
Description string `json:"description"`
EmploymentStatus string `json:"employmentStatus"`
ExternalJobPostingId string `json:"externalJobPostingId"`
ListedAt int `json:"listedAt"`
JobPostingOperationType string `json:"jobPostingOperationType"`
Title string `json:"title"`
Location string `json:"location"`
WorkplaceTypes []string `json:"workplaceTypes"`
}
var (
PostURL = "https://api.linkedin.com/v2/ugcPosts?oauth2_access_token="
)
/*
Have ProfileID, Share any post what you want.
*/
func (ln *Linkedin) SharePost(token, id, text string) error {
postUrl := fmt.Sprintf("%s%s", PostURL, token)
data := Post{
Author: "urn:li:person:" + id,
LifeCycleState: "PUBLISHED",
SpecificContent: SpecificContent{
ShareContent: ShareContent{
ShareCommentary: ShareCommentary{
Text: text,
},
ShareMediaCategory: "NONE",
},
},
Visibility: Visibility{
Code: "PUBLIC",
},
}
post, err := postToJson(&data)
if err != nil {
return fmt.Errorf("error occurred = %s", err.Error())
}
resp, err := http.Post(postUrl, "application/json", bytes.NewReader(post))
if err != nil {
return fmt.Errorf("error occurred = %s", err.Error())
}
_, err = io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error occurred = %s", err.Error())
}
return nil
}
/*
Convert Post type to []byte type.
*/
func postToJson(post *Post) ([]byte, error) {
data, err := json.Marshal(post)
if err != nil {
return nil, err
}
return data, nil
}