0
Retrieve Comments On Shares
One script reply has been approved by the moderators Verified

Retrieve comments on shares given the share urn. See the docs here

Created by hugo697 158 days ago Viewed 195 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 158 days ago
1
type Linkedin = {
2
	token: string
3
	apiVersion: string
4
}
5

6
export async function main(resource: Linkedin, entityUrn: string, maxResults: number = 100) {
7
	const count = 50 // Number of results per request
8
	const results: any[] = []
9
	const encodedEntityUrn = encodeURIComponent(entityUrn)
10

11
	let params = new URLSearchParams({
12
		count: count.toString(),
13
		start: '0'
14
	})
15

16
	let done = false
17
	do {
18
		const endpoint = `https://api.linkedin.com/rest/socialActions/${encodedEntityUrn}/comments?${params.toString()}`
19

20
		const response = await fetch(endpoint, {
21
			method: 'GET',
22
			headers: {
23
				Authorization: `Bearer ${resource.token}`,
24
				'X-Restli-Protocol-Version': '2.0.0',
25
				'LinkedIn-Version': `${resource.apiVersion}`
26
			}
27
		})
28

29
		if (!response.ok) {
30
			throw new Error(`HTTP error! status: ${response.status}`)
31
		}
32

33
		const data = await response.json()
34
		results.push(...data.elements)
35

36
		params.set('start', (parseInt(params.get('start')!) + count).toString())
37
		if (data.elements.length < count || results.length >= maxResults) {
38
			done = true
39
		}
40
	} while (!done)
41

42
	return results.slice(0, maxResults)
43
}
44