Search code examples
linkedin-apiamazon-ecs

Creating a Linkedin image ugcPost works when posting from localhost but not from AWS ECS


I am creating image shares on company profiles in my java app following the docs here --->https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin#create-an-image-share

The problem that I'm encountering is that after uploading the file successfully(I get 201), from AWS ECS FARGATE container,but posting is successful from localhost. this is my code below:

  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 is the result received from registering the media upload by calling POST https://api.linkedin.com/v2/assets?action=registerUpload

I'm checking the asset status using /v2/assets/{asset-id} and I ultimately get:

{
    "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"
}

But when running from localhost everything works as expected and post shows up on company feed.

Really struggling to understand what could be the issue.


Solution

  • I actually managed to solve the issue in the meantime. And the issue was caused by the fact that fileUrl was a link to an file in a s3 bucket linked as an origin to a cloudfront deployment to which I had direct access. So I used the AmazonS3 s3client to get the inputstream directly.