Image share with linkedin v2 api not posting on page feed

只愿长相守 提交于 2019-12-01 08:26:34

Not familiar with Java, but I had the same problem using Ruby and I fixed it by adding the MIME type of the image I was uploading as the Content-Type in the request headers. So in your specific case it would be:

request.addHeader("Content-Type", "image/png");

Also take a look at my solution using Ruby's RestClient and Minimagick: https://stackoverflow.com/a/54902863/7844946

Ok I've solved it, if anyone encounters the same problem, this is what i did wrong. In the request i added a multipart to request body, this is wrong, you just go RAW. So instead of

    File file = new File(pictureUrl);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addBinaryBody("upload-file", file);
    request.setEntity(builder.build());

you just put

request.setEntity(new FileEntity(new File(pictureUrl), ContentType.create(picture.getContentType())));

and then everything goes on ok.

I found curl to C# converter https://curl.olsh.me/ below is code snippet is generated:



    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("PUT"), "https://api.linkedin.com/mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtBiZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-IhpbfXrRow1"))
        {
            request.Headers.TryAddWithoutValidation("Authorization", "Bearer redacted"); 

            request.Content = new ByteArrayContent(File.ReadAllBytes("/test.png")); 

            var response = await httpClient.SendAsync(request);
        }
    }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!