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}`)
}
}
export async function main(
resource: Linkedin,
postData: {
author: string
commentary: string
visibility: 'CONNECTIONS' | 'PUBLIC' | 'LOGGED_IN' | 'CONTAINER'
distribution: {
feedDistribution: 'NONE' | 'MAIN_FEED'
}
content: {
media: {
id: string
title?: string
altText?: string
}
}
isReshareDisabledByAuthor: boolean
},
image: Base64
) {
// Step 1: Initialize image upload
const uploadData = await initializeImageUpload(resource, postData.author)
const uploadUrl = uploadData.uploadUrl
const imageUrn = uploadData.image
// Step 2: Upload the image
await uploadImage(uploadUrl, image)
// Step 3: Create post with image
postData.content.media.id = imageUrn
const endpoint = 'https://api.linkedin.com/rest/posts'
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({ ...postData, lifecycleState: 'PUBLISHED' })
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const postId = response.headers.get('x-restli-id')
const data = await response.json()
return { ...data, postId }
}
Submitted by hugo697 129 days ago
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}`)
}
}
export async function main(
resource: Linkedin,
postData: {
author: string
commentary: string
visibility: 'CONNECTIONS' | 'PUBLIC' | 'LOGGED_IN' | 'CONTAINER'
distribution: {
feedDistribution: 'NONE' | 'MAIN_FEED'
}
content: {
media: {
id: string
title?: string
altText?: string
}
}
isReshareDisabledByAuthor: boolean
},
image: Base64
) {
// Step 1: Initialize image upload
const uploadData = await initializeImageUpload(resource, postData.author)
const uploadUrl = uploadData.uploadUrl
const imageUrn = uploadData.image
// Step 2: Upload the image
await uploadImage(uploadUrl, image)
// Step 3: Create post with image
postData.content.media.id = imageUrn
const endpoint = 'https://api.linkedin.com/rest/posts'
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({ ...postData, lifecycleState: 'PUBLISHED' })
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const postId = response.headers.get('x-restli-id')
const data = await response.json()
return { ...data, postId }
}
Submitted by hugo697 130 days ago