-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.js
More file actions
115 lines (106 loc) · 3.98 KB
/
Copy pathexample.js
File metadata and controls
115 lines (106 loc) · 3.98 KB
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
'use-strict'
// This sample will post a basic message in your LinkedIn profile
const https = require('https');
const accessToken = require('./token.json').access_token;
// ---------------------------------------------------------------------------------------------------------------------
// Example
// ---------------------------------------------------------------------------------------------------------------------
let title = "Hello World!";
let text = "This is my first post!";
let shareUrl = "https://www.example.com/content.html"
let shareThumbnailUrl = "https://www.example.com/image.jpg"
getLinkedinId(accessToken).then(ownerId => {
postShare(accessToken, ownerId, title, text, shareUrl, shareThumbnailUrl).then(r => {
console.log(r); // status 201 signal successful posting
}).catch(e => console.log(e));
}).catch(e => console.log(e));
// ---------------------------------------------------------------------------------------------------------------------
// Generic Node.js API to post on LinkedIn
// ---------------------------------------------------------------------------------------------------------------------
// Get LinkedIn ID, i.e. ownerId
function getLinkedinId(accessToken) {
return new Promise((res, rej) => {
let hostname = 'api.linkedin.com';
let path = '/v2/me';
let method = 'GET';
let headers = {
'Authorization': 'Bearer ' + accessToken,
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0'
};
let body = ''
_request(method, hostname, path, headers, body).then(r => {
res(JSON.parse(r.body).id)
}).catch(e => rej(e))
})
}
// Publish content on LinkedIn
function postShare(accessToken, ownerId, title, text, shareUrl, shareThumbnailUrl) {
return new Promise((res, rej) => {
let hostname = 'api.linkedin.com';
let path = '/v2/shares';
let method = 'POST';
let body = {
"owner": "urn:li:person:" + ownerId,
"subject": title,
"text": {
"text": text // max 1300 characters
},
"content": {
"contentEntities": [{
"entityLocation": shareUrl,
"thumbnails": [{
"resolvedUrl": shareThumbnailUrl
}]
}],
"title": title
},
"distribution": {
"linkedInDistributionTarget": {}
}
}
let headers = {
'Authorization': 'Bearer ' + accessToken,
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0',
'Content-Type': 'application/json',
'x-li-format': 'json',
'Content-Length': Buffer.byteLength(JSON.stringify(body))
};
_request(method, hostname, path, headers, JSON.stringify(body)).then(r => {
res(r);
}).catch(e => rej(e))
})
}
// Generic HTTP requester
function _request(method, hostname, path, headers, body) {
return new Promise((resolve, reject) => {
let reqOpts = {
method,
hostname,
path,
headers,
"rejectUnauthorized": false // WARNING: accepting unauthorised end points for testing ONLY
};
let resBody = "";
let req = https.request(reqOpts, res => {
res.on('data', data => {
resBody += data.toString('utf8');
});
res.on('end', () => {
resolve({
"status": res.statusCode,
"headers": res.headers,
"body": resBody
})
});
});
req.on('error', e => {
reject(e);
});
if (method !== 'GET') {
req.write(body);
}
req.end();
})
}