LinkedIn Posts API response is OK but post not showing

Viewed 60

This issue only occurs with posts with multipart video, Posts with images and videos below 4MB are working, when posting to Posts API i get a response with Status OK and the post URN in the header but when i try getting the post data using the given URN i'm getting error 404.

Here is the function that i am using to upload the video to LinkedIn.

const uploadVideo = async (linkedinId, accessToken, videoUrl) => {
  /* Fetching video from Google storage */
  const videoData = await axios.get(videoUrl, { responseType: 'arraybuffer' });
  const contentType = videoData.headers['content-type'];
  const videoSize = videoData.headers['content-length'];

  const url = 'https://api.linkedin.com/rest/videos';
  const body = {
    initializeUploadRequest: {
      owner: `urn:li:organization:${linkedinId}`,
      fileSizeBytes: Number(videoSize),
    },
  };

  const headers = {
    Authorization: `Bearer ${accessToken}`,
    'X-Restli-Protocol-Version': '2.0.0',
    'x-li-format': 'json',
    'LinkedIn-Version': 202207,
  };

  const response = await axios.post(url, body, { headers, params: { action: 'initializeUpload' } });
  const { uploadInstructions } = response.data.value;
  const asset = response.data.value.video;

  /* Uploading video */
  try {
    const uploadPromises = uploadInstructions.map(async ({ uploadUrl, firstByte, lastByte }) => {
      const arrayBuffer = videoData.data.slice(firstByte, lastByte);

      return axios({
        url: uploadUrl,
        method: 'POST',
        data: arrayBuffer,
        headers: {
          'Content-Type': contentType,
        },
        maxBodyLength: Infinity,
        maxContentLength: Infinity,
      });
    });

    const uploadResponses = await Promise.all(uploadPromises);

    const finalizeUploadBody = {
      finalizeUploadRequest: {
        video: asset,
        uploadToken: '',
        uploadedPartIds: uploadResponses.map((uploadResponse) => uploadResponse.headers.etag),
      },
    };

    await axios.post(url, finalizeUploadBody, {
      headers: {
        ...headers,
        'Content-Type': 'application/json',
      },
      params: {
        action: 'finalizeUpload',
      },
    });

  } catch (error) {
    throw error;
  }

  return asset;
};

Here is the function that i am using to publish the post to LinkedIn.

const publishContent = async (
  linkedinId,
  accessToken,
  media,
) => {
  const url = 'https://api.linkedin.com/rest/posts';
  const body = {
    author: `urn:li:organization:${linkedinId}`,
    commentary: 'content',
    visibility: 'PUBLIC',
    lifecycleState: 'PUBLISHED',
    distribution: {
      feedDistribution: 'MAIN_FEED',
    },
  };

  const asset = await uploadVideo(linkedinId, accessToken, media.urls[0], isPage);

  body.content = {
    media: {
      title: 'Title',
      id: asset,
    },
  };

  const headers = {
    Authorization: 'Bearer ' + accessToken,
    'X-Restli-Protocol-Version': '2.0.0',
    'x-li-format': 'json',
    'LinkedIn-Version': 202207,
  };

  return axios.post(url, body, { headers });
};
0 Answers
Related