创建 Linkedin 图片 ugcPost 在从本地主机而不是从 AWS ECS 发布时有效
Creating a Linkedin image ugcPost works when posting from localhost but not from AWS ECS
我正在按照此处的文档在我的 java 应用中的公司简介中创建图像共享 --->https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin#create-an-image-share
我遇到的问题是,从 AWS ECS FARGATE 容器成功上传文件(我得到 201)后,posting 从本地主机成功。这是我的代码:
String mediaUploadResponse = registerMediaUpload(userId, headers);
JsonNode mediaUpload = objectMapper.readTree(mediaUploadResponse);
String uploadUrl = mediaUpload.findPath("uploadUrl").asText();
HttpClient client = HttpClientBuilder.create().build();
HttpPut request = new HttpPut(uploadUrl);
request.addHeader("Content-Type", APPLICATION_OCTET_STREAM_VALUE);
request.setHeader("X-Restli-Protocol-Version", "2.0.0");
request.setHeader("Authorization", requireNonNull(headers.get(AUTHORIZATION)).get(0));
Path tempFilePath = Files.createTempFile("linkedin", null);
try (InputStream fileStream = new URL(fileUrl).openStream()) {
Files.write(tempFilePath, fileStream.readAllBytes());
File tempFile = tempFilePath.toFile();
request.setEntity(new FileEntity(tempFile, IMAGE_PNG));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 201) {
log.info("------------------- media upload result {}", response.getEntity());
return mediaUpload;
} else {
log.error("linkedin media upload request failed {}", request);
throw new BadRequestException(response.toString());
}
} finally {
boolean deleted = tempFilePath.toFile().delete();
log.info("------------------- tempfile deleted: {}", deleted);
}
mediaUpload
是调用POST https://api.linkedin.com/v2/assets?action=registerUpload
注册媒体上传得到的结果
我正在使用 /v2/assets/{asset-id} 检查资产状态,我最终得到:
{
"recipes": [
{
"recipe": "urn:li:digitalmediaRecipe:feedshare-image",
"status": "CLIENT_ERROR"
}
],
"serviceRelationships": [
{
"relationshipType": "OWNER",
"identifier": "urn:li:userGeneratedContent"
}
],
"mediaTypeFamily": "STILLIMAGE",
"created": 1588963432407,
"id": "C4D22AQGIhdXwlSvDZQ",
"lastModified": 1588963433173,
"status": "ALLOWED"
}
但是当来自本地主机的 运行 一切正常时,post 出现在公司提要中。
真的很难理解可能是什么问题。
我实际上设法同时解决了这个问题。问题是由于 fileUrl
是一个 link 到 s3 存储桶中的文件 link 作为我可以直接访问的云端部署的来源。所以我使用 AmazonS3 s3client 直接获取输入流。
我正在按照此处的文档在我的 java 应用中的公司简介中创建图像共享 --->https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin#create-an-image-share
我遇到的问题是,从 AWS ECS FARGATE 容器成功上传文件(我得到 201)后,posting 从本地主机成功。这是我的代码:
String mediaUploadResponse = registerMediaUpload(userId, headers);
JsonNode mediaUpload = objectMapper.readTree(mediaUploadResponse);
String uploadUrl = mediaUpload.findPath("uploadUrl").asText();
HttpClient client = HttpClientBuilder.create().build();
HttpPut request = new HttpPut(uploadUrl);
request.addHeader("Content-Type", APPLICATION_OCTET_STREAM_VALUE);
request.setHeader("X-Restli-Protocol-Version", "2.0.0");
request.setHeader("Authorization", requireNonNull(headers.get(AUTHORIZATION)).get(0));
Path tempFilePath = Files.createTempFile("linkedin", null);
try (InputStream fileStream = new URL(fileUrl).openStream()) {
Files.write(tempFilePath, fileStream.readAllBytes());
File tempFile = tempFilePath.toFile();
request.setEntity(new FileEntity(tempFile, IMAGE_PNG));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 201) {
log.info("------------------- media upload result {}", response.getEntity());
return mediaUpload;
} else {
log.error("linkedin media upload request failed {}", request);
throw new BadRequestException(response.toString());
}
} finally {
boolean deleted = tempFilePath.toFile().delete();
log.info("------------------- tempfile deleted: {}", deleted);
}
mediaUpload
是调用POST https://api.linkedin.com/v2/assets?action=registerUpload
我正在使用 /v2/assets/{asset-id} 检查资产状态,我最终得到:
{
"recipes": [
{
"recipe": "urn:li:digitalmediaRecipe:feedshare-image",
"status": "CLIENT_ERROR"
}
],
"serviceRelationships": [
{
"relationshipType": "OWNER",
"identifier": "urn:li:userGeneratedContent"
}
],
"mediaTypeFamily": "STILLIMAGE",
"created": 1588963432407,
"id": "C4D22AQGIhdXwlSvDZQ",
"lastModified": 1588963433173,
"status": "ALLOWED"
}
但是当来自本地主机的 运行 一切正常时,post 出现在公司提要中。
真的很难理解可能是什么问题。
我实际上设法同时解决了这个问题。问题是由于 fileUrl
是一个 link 到 s3 存储桶中的文件 link 作为我可以直接访问的云端部署的来源。所以我使用 AmazonS3 s3client 直接获取输入流。