Edits history of script submission #8869 for ' Create Comment (linkedin)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Linkedin = {
    	token: string
    	apiVersion: string
    }
    
    type Base64 = string
    
    async function initializeImageUpload(resource: Linkedin, owner: string) {
    	const endpoint = 'https://api.linkedin.com/rest/images?action=initializeUpload'
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			Authorization: `Bearer ${resource.token}`,
    			'X-Restli-Protocol-Version': '2.0.0',
    			'Content-Type': 'application/json',
    			'LinkedIn-Version': `${resource.apiVersion}`
    		},
    		body: JSON.stringify({
    			initializeUploadRequest: {
    				owner
    			}
    		})
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    	return data.value
    }
    
    async function uploadImage(uploadUrl: string, image: Base64) {
    	const buffer = Buffer.from(image, 'base64')
    
    	const response = await fetch(uploadUrl, {
    		method: 'PUT',
    		headers: {
    			'Content-Type': 'application/octet-stream'
    		},
    		body: buffer
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    }
    
    async function uploadImageForComment(resource: Linkedin, owner: string, image: Base64) {
    	const uploadData = await initializeImageUpload(resource, owner)
    	const uploadUrl = uploadData.uploadUrl
    	const imageUrn = uploadData.image
    
    	await uploadImage(uploadUrl, image)
    
    	return imageUrn
    }
    
    export async function main(
    	resource: Linkedin,
    	activityUrn: string, // can be either Share/Post/Comment Urn
    	commentData: {
    		actor: string
    		object: string
    		message: {
    			text: string
    		}
    		content?: [
    			{
    				entity: {
    					// Different argument for versioned API.
    					digitalmediaAsset: string // Not Base64, has to be a URN returned by the Image API.
    				}
    			}
    		]
    		parentComment?: string
    	},
    	image?: Base64
    ) {
    	if (image) {
    		const imageUrn = (await uploadImageForComment(resource, commentData.actor, image)).replace(
    			'image',
    			'digitalmediaAsset'
    		)
    
    		commentData.content = [
    			{
    				entity: {
    					digitalmediaAsset: imageUrn
    				}
    			}
    		]
    	}
    
    	const encodedActivityUrn = encodeURIComponent(activityUrn)
    	const endpoint = `https://api.linkedin.com/v2/socialActions/${encodedActivityUrn}/comments`
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			Authorization: `Bearer ${resource.token}`,
    			'X-Restli-Protocol-Version': '2.0.0',
    			'Content-Type': 'application/json'
    		},
    		body: JSON.stringify(commentData)
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 689 days ago

  • nativets
    type Linkedin = {
    	token: string
    	apiVersion: string
    }
    
    type Base64 = string
    
    async function initializeImageUpload(resource: Linkedin, owner: string) {
    	const endpoint = 'https://api.linkedin.com/rest/images?action=initializeUpload'
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			Authorization: `Bearer ${resource.token}`,
    			'X-Restli-Protocol-Version': '2.0.0',
    			'Content-Type': 'application/json',
    			'LinkedIn-Version': `${resource.apiVersion}`
    		},
    		body: JSON.stringify({
    			initializeUploadRequest: {
    				owner
    			}
    		})
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    	return data.value
    }
    
    async function uploadImage(uploadUrl: string, image: Base64) {
    	const buffer = Buffer.from(image, 'base64')
    
    	const response = await fetch(uploadUrl, {
    		method: 'PUT',
    		headers: {
    			'Content-Type': 'application/octet-stream'
    		},
    		body: buffer
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    }
    
    async function uploadImageForComment(resource: Linkedin, owner: string, image: Base64) {
    	const uploadData = await initializeImageUpload(resource, owner)
    	const uploadUrl = uploadData.uploadUrl
    	const imageUrn = uploadData.image
    
    	await uploadImage(uploadUrl, image)
    
    	return imageUrn
    }
    
    export async function main(
    	resource: Linkedin,
    	activityUrn: string, // can be either Share/Post/Comment Urn
    	commentData: {
    		actor: string
    		object: string
    		message: {
    			text: string
    		}
    		content?: [
    			{
    				entity: {
    					// Different argument for versioned API.
    					digitalmediaAsset: string // Not Base64, has to be a URN returned by the Image API.
    				}
    			}
    		]
    		parentComment?: string
    	},
    	image?: Base64
    ) {
    	if (image) {
    		const imageUrn = (await uploadImageForComment(resource, commentData.actor, image)).replace(
    			'image',
    			'digitalmediaAsset'
    		)
    
    		commentData.content = [
    			{
    				entity: {
    					digitalmediaAsset: imageUrn
    				}
    			}
    		]
    	}
    
    	const encodedActivityUrn = encodeURIComponent(activityUrn)
    	const endpoint = `https://api.linkedin.com/v2/socialActions/${encodedActivityUrn}/comments`
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			Authorization: `Bearer ${resource.token}`,
    			'X-Restli-Protocol-Version': '2.0.0',
    			'Content-Type': 'application/json'
    		},
    		body: JSON.stringify(commentData)
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 689 days ago