与 linkedin v2 api 的图像共享未在页面 Feed 上发布
Image share with linkedin v2 api not posting on page feed
好的,这是我的问题.. 要通过 linkedin api 共享图片 post,您首先必须注册您的图片文件,您可以通过 post 请求来完成在其中发送二进制文件。然后您使用原始请求中的图像 URN 来提交您的 post。我的请求通过了 returns 201 代码(这应该是一个成功的请求)但最终没有 post 图像或文本。如果我尝试 post 仅文本,它会起作用。我试过使用 curl 注册我的图像,它在 linkedin 上 posted,所以我认为我没有在请求中正确发送二进制文件,这是我的请求:
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,但它仍然没有 post。
这是他们在 Share API doc 上提出的请求的 curl 示例。
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 等效项有什么问题吗?
编辑:忘了说我什至尝试从 java 调用 curl,与我在终端中使用的代码相同,但它仍然不起作用..
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;
}
输出返回一个空字符串。
Edit2: 另一个有趣的事情是,当我执行主请求时,我在其中发送文本和提交图像后得到的媒体缸,我再次得到 201,好像成功了,在响应中我什至获取 post ID。然后我尝试使用另一个 api 端点并使用我从响应中获得的 id 提取 post,然后我得到所有数据,例如 post 是 post编辑。它甚至在 json 中说我知道生命周期已发布并且媒体的状态已准备就绪。与 linkedin 上的 json 图像 post 唯一不同的是媒体对象有缩略图,在这种情况下它们没有,它只是一个空的 json 数组。
不熟悉 Java,但我在使用 Ruby 时遇到了同样的问题,我通过在请求 headers。因此,在您的特定情况下,它将是:
request.addHeader("Content-Type", "image/png");
另请查看我使用 Ruby 的 RestClient 和 Minimagick 的解决方案:https://whosebug.com/a/54902863/7844946
好的,我已经解决了,如果有人遇到同样的问题,那就是我做错了。在请求中我添加了一个多部分请求正文,这是错误的,你只是去原始。所以而不是
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())));
然后一切正常。
我找到了 curl 到 C# 的转换器 https://curl.olsh.me/
下面是生成的代码片段:
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);
}
}
我是用 kotlin 编写的,在为同样的问题苦苦挣扎了很长时间后,我设法解决了它,对于那些为类似问题苦苦挣扎的人,我将在下面留下我的示例代码。对于visual你需要提供一个直接的URI路径,还要注意header结构。
这是代码。
var file = File(ImageURI)
val urls = URL(url)
var connection: HttpsURLConnection = urls.openConnection() as HttpsURLConnection
connection.setRequestProperty("Authorization","Bearer " + accesToken)
connection.setRequestProperty("Content-Type", "image/png")
connection.setRequestProperty("cache-control", "no-cache")
connection.setRequestProperty("X-Restli-Protocol-Version", "2.0.0")
connection.requestMethod = "POST"
connection.doOutput = true
connection.doInput = true
var request = DataOutputStream(connection.outputStream)
request.write(file.readBytes())
request.flush()
println("Response: "+connection.responseCode)
好的,这是我的问题.. 要通过 linkedin api 共享图片 post,您首先必须注册您的图片文件,您可以通过 post 请求来完成在其中发送二进制文件。然后您使用原始请求中的图像 URN 来提交您的 post。我的请求通过了 returns 201 代码(这应该是一个成功的请求)但最终没有 post 图像或文本。如果我尝试 post 仅文本,它会起作用。我试过使用 curl 注册我的图像,它在 linkedin 上 posted,所以我认为我没有在请求中正确发送二进制文件,这是我的请求:
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,但它仍然没有 post。 这是他们在 Share API doc 上提出的请求的 curl 示例。
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 等效项有什么问题吗?
编辑:忘了说我什至尝试从 java 调用 curl,与我在终端中使用的代码相同,但它仍然不起作用..
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;
}
输出返回一个空字符串。
Edit2: 另一个有趣的事情是,当我执行主请求时,我在其中发送文本和提交图像后得到的媒体缸,我再次得到 201,好像成功了,在响应中我什至获取 post ID。然后我尝试使用另一个 api 端点并使用我从响应中获得的 id 提取 post,然后我得到所有数据,例如 post 是 post编辑。它甚至在 json 中说我知道生命周期已发布并且媒体的状态已准备就绪。与 linkedin 上的 json 图像 post 唯一不同的是媒体对象有缩略图,在这种情况下它们没有,它只是一个空的 json 数组。
不熟悉 Java,但我在使用 Ruby 时遇到了同样的问题,我通过在请求 headers。因此,在您的特定情况下,它将是:
request.addHeader("Content-Type", "image/png");
另请查看我使用 Ruby 的 RestClient 和 Minimagick 的解决方案:https://whosebug.com/a/54902863/7844946
好的,我已经解决了,如果有人遇到同样的问题,那就是我做错了。在请求中我添加了一个多部分请求正文,这是错误的,你只是去原始。所以而不是
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())));
然后一切正常。
我找到了 curl 到 C# 的转换器 https://curl.olsh.me/ 下面是生成的代码片段:
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);
}
}
我是用 kotlin 编写的,在为同样的问题苦苦挣扎了很长时间后,我设法解决了它,对于那些为类似问题苦苦挣扎的人,我将在下面留下我的示例代码。对于visual你需要提供一个直接的URI路径,还要注意header结构。
这是代码。
var file = File(ImageURI)
val urls = URL(url)
var connection: HttpsURLConnection = urls.openConnection() as HttpsURLConnection
connection.setRequestProperty("Authorization","Bearer " + accesToken)
connection.setRequestProperty("Content-Type", "image/png")
connection.setRequestProperty("cache-control", "no-cache")
connection.setRequestProperty("X-Restli-Protocol-Version", "2.0.0")
connection.requestMethod = "POST"
connection.doOutput = true
connection.doInput = true
var request = DataOutputStream(connection.outputStream)
request.write(file.readBytes())
request.flush()
println("Response: "+connection.responseCode)