1

I have created image share upload url using "https://api.linkedin.com/v2/assets?action=registerUpload" and using that upload url i am trying to upload image to linked-in but i am getting Bad request 400 error from API, please find below code snippet,

 private void UploadImageToLinkedIn(string accessToken)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
                client.DefaultRequestHeaders.Add("X-Restli-Protocol-Version", "2.0.0");
                byte[] imgBytes = System.IO.File.ReadAllBytes("D:\\work\\All_Photos\\1_full.jpg");

                using (MultipartFormDataContent fd = new MultipartFormDataContent())
                {
                    var fileContent = new ByteArrayContent(imgBytes);
                    fileContent.Headers.Add("Content-Type", "multipart/form-data");
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "1_full" };
                    fd.Add(fileContent);

                    string postingURL = "https://api.linkedin.com/mediaUpload/C4E22AQFg1jP-2n4Jog/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQLl5BnZw1Rk0wAAAWk5k9KsK.........";

                    var result = client.PostAsync(postingURL, fd).Result;
                }
            }
        }

I have also tried it on postman with no luck. Can anyone help me out?

  • I don't know C#, but looking at your code, content-type needs to be the MIME type of the image you're uploading (i.e. image/jpeg). Also I'm seeing a multipart-form request, which according to this answer is wrong: https://stackoverflow.com/a/54944873/7844946 – Ervin Kalemi Mar 01 '19 at 16:08

1 Answers1

0

You can upload image to LinkedIn using upload url once you register the upload. 1. Content type should be application/binary 2.image path sould be absolute path. Eg : users/desktop/myimage.jpg I made it work with curl.

augustine jenin
  • 424
  • 4
  • 10