Search code examples
node.jslinkedin-api

Why am I getting 404 ("Resource media not found") on the media upload from the docs?


The docs for uploading rich media to LinkedIn (https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/rich-media-shares#upload-rich-media) say to make a POST to https://api.linkedin.com/media/upload with form data. As far as I can tell I am doing that correctly using request-promise on my Node server, but I am still getting a 404.

Initially I had a problem with my file, but now I think I am properly creating a Buffer. Even if I'm not, that was preventing me from even making the request, and now I am and I don't think that would cause a 404.

I have also tried using 1.0.0 and 2.0.0 versions of the X-Restli-Protocol-Version (LinkedIn API thing).

// See LinkedIn docs on Rich Media shares https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/rich-media-shares

const stream = require('stream');
const rp = require('request-promise')

async function postRichMediaShare(accessToken) {
  try {
    const file = await rp({
      method: 'get',
      url: 'https://local-image-bucket.s3.amazonaws.com/Artboard+copy.png'
    });

    // Buffer magic
    const buffer = new Buffer.from(file);
    const bufferStream = new stream.PassThrough();
    bufferStream.end( buffer );
    bufferStream.pipe( process.stdout );

    const options = {
      method: 'post',
      url: 'https://api.linkedin.com/v2/media/upload',
      headers: { 'X-Restli-Protocol-Version': '2.0.0',
      "Authorization": `Bearer ${accessToken}` },
      formData: {
        file: {
          value: bufferStream,
          options: {
            filename: 'Artboard+copy.png',
            contentType: 'image/png'
          }
        }
      },
    };

    const response = await rp(options);
    console.log("response", response);

    return response;
  } catch (error) {
    throw new Error(error);
  }
}

Instead of the response suggested in the docs, I'm getting this error message from LinkedIn:

error: "{"serviceErrorCode":0,"message":"Resource media does not exist","status":404}"


Solution

  • I'm an idiot. 404 should be expected because I'm requesting https://api.linkedin.com/v2/media/upload and the docs say https://api.linkedin.com/media/upload (no v2/). I believe every other call is versioned. Perhaps an empowered LinkedIn employee reading this could make a route for v2/ that does all the same stuff.

    Note, there may be other problems with the code above, I am still struggling but now I'm working on things outside the scope of this question about 404.