与linkedin v2 api共享的图像未发布在页面供稿上 [英] Image share with linkedin v2 api not posting on page feed

查看:55
本文介绍了与linkedin v2 api共享的图像未发布在页面供稿上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以这是我的问题..要通过linkedin api共享图像发布,您首先必须注册图像文件,然后通过发送二进制文件的发布请求来完成此操作.然后,您可以使用原始请求中的图片URN来提交您的帖子.我的请求通过,返回201代码(应该是成功的请求),但最终没有发布图像或文本.如果我尝试仅发布文本,则可以.我尝试使用curl注册我的图像,并将其发布在linkedin上,所以我认为我没有在请求中正确发送二进制文件,这是我的请求:

Ok so this is my problem.. To share an image post via linkedin api, you first have to register your image file, you do that via a post request in which you send your binary file. Then you use the the image URN in the original request to submit your post. My request goes through, returns 201 code (which should be a successful request) but ends up not posting the image or the text. If i try to post only text, it works. I've tried registering my image using curl, and it posted on linkedin, so i think i'm not sending the binary file in a request properly, this is my request:

            HttpClient client = HttpClientBuilder.create().build();
            HttpPut request = new HttpPut(uploadUrl);
            request.addHeader("Content-Type", "data/binary");
            request.setHeader("X-Restli-Protocol-Version", "2.0.0");
            request.setHeader("Authorization", "Bearer " + myToken);

            File file = new File(pictureUrl);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.addBinaryBody("upload-file", file);
            request.setEntity(builder.build());
            HttpResponse response = client.execute(request);

我收到带有此代码的代码201,但仍未发布. 这是他们在

I get code 201 with this code, but it still doesn't post. This is the curl example of the request that they give on Share API doc.

curl -i --upload-file /Users/peter/Desktop/superneatimage.png --header "Authorization: Bearer redacted" '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'

您能告诉我我的Java等同程序出了什么问题吗?

Can you tell me what is wrong with my java equivalent?

忘了说我什至尝试从java调用curl,这与我在终端中使用的相同代码,但仍然无法正常工作.

Forgot to say i even tried calling curl from java, the same code i used in the terminal, and it still didn't work..

        Process p;
        try {
            p = Runtime.getRuntime().exec("curl -i --upload-file" + " " + pictureUrl + " " + "--header \"Authorization: Bearer " + myToken + "\" '" + uploadUrl + "'");
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            String outputController = "";
            while ((line = reader.readLine()) != null) {
                outputController = outputController + '\n' + line;
            }
            System.out.println("out: ");
            System.out.println(outputController);
            return true;
        } catch (IOException | InterruptedException ex) {
            return false;
        }

输出返回一个空字符串.

Output returned an empty String.

Edit2:另一个有趣的事情是,当我执行主要请求时,在其中发送文本以及提交图像后收到的媒体media,我再次获得201,就像它成功一样,并且在响应中我什至得到了邮递区号.然后,我尝试使用另一个api端点,并使用从响应中获取的ID提取该帖子,然后获取所有数据,就像该帖子一样.它甚至在json中说我知道生命周期已发布,并且媒体的状态为READY.唯一与json上发布的图像帖子不同的是,媒体对象具有缩略图,在这种情况下,它们没有缩略图,只是一个空的json数组.

Another funny thing, when i execute the main request, in which i send the text, and media urns that i get after submitting images, i get 201 again, like it's successful, and in the response i even get the post id. Then i try to use the another api endpoint and pull that post using the id i got from the response, and i get all the data, like the post is posted. It even says in the json that i get that lifecycle is PUBLISHED and the status of the medias is READY. Only thing that's different from the json an image post that is on linkedin is that the media object have thumbnails, and in this case they don't, it's just an empty json array.

推荐答案

好的,我已经解决了,如果有人遇到相同的问题,这就是我做错的.在请求中,我向请求正文添加了一个多部分,这是错误的,您只需要进行RAW处理即可.因此,而不是

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());

您刚刚放

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

然后一切正常.

这篇关于与linkedin v2 api共享的图像未发布在页面供稿上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆