2

I want to know how to make a proper 'POST' request to upload an image as a binary file to Linkedin via their API. This is what they say in their documentation (https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context):

Using the uploadUrl returned from Step 1, upload your image to LinkedIn. To upload your image, send a POST request to the uploadUrl with your image included as a binary file.

They then proceed to give an example using cURL but I need to make an HTTP request from my Apex code so I need the json format of the request body.

To further illustrate my point, these are the steps to post an image in Linkedin through their API:

1) Register your image to be uploaded.

2) Upload your image to LinkedIn.

3) Create the image share.

They provide the json for steps 1 and 3 but not for 2, hence my problem.

This is the part of the code I consider relevant, should you need any other part just tell me so and I will edit my question:

        httpReq.setMethod('POST');
        httpReq.setEndpoint(uploadUrl);
        httpReq.setHeader('Authorization', 'Bearer '+linkedlnInfoListNew[0].Access_Token__c);        
        httpReq.setHeader('Content-Type', 'application/binary');             
        httpReq.setHeader('X-Restli-Protocol-Version', '2.0.0');
        requestBody = EncodingUtil.base64Encode(banner);
        httpReq.setBody(requestBody);

The uploadUrl variable is the URL provided in the response of step 1. As it is, this request produces no response body and has a status: created and status code: 201

I have tried the following:

To summarize, I just want to know what to put in the body of the 'POST' request mentioned in step 2 of the Linkedin API documentation on how to share an image.

EDIT: It would also be helpful if someone knows the answer to this problem but instead of using the binary of the image they use an url referencing the image.

EDIT 21/07/19: This is the image I am currently trying to share on Linkedin.

enter image description here

This is the updated http request with its headers:

    httpReq.setEndpoint(uploadUrl);
    httpReq.setBodyAsBlob(formBlob);        
    httpReq.setHeader('Connection', 'keep-alive');
    httpReq.setHeader('Content-Length', contentLength);
    httpReq.setHeader('Content-Type', contentType);
    httpReq.setMethod('POST');
    httpReq.setTimeout(120000);             //These are all the attributes that are set in the example.

    httpReq.setHeader('Authorization', 'Bearer '+linkedlnInfoListNew[0].Access_Token__c);            
    //httpReq.setHeader('x-li-format' , 'json');
    httpReq.setHeader('X-Restli-Protocol-Version', '2.0.0');

The formBlob variable is the blob of the image constructed using the code provided in this answer.

And this is the response given after the request above:

System.HttpResponse[Status=Request Entity Too Large, StatusCode=413]

Thank you for your time.

0
0

Content-Type needs to be multipart/form-data; there are also other requirements (see docs). This is difficult to do in Apex but possible. You should vote up this idea.

14
  • Thanks for the answer! I was using this doc to share the image. What's the difference between the rich media share and the image share in the documentation I was using? For both scenarios I have the same question: in both it says to make a post request to a certain address but they do not specify what to include in the body of said request. Do you know this? – Zufra Jul 13 '19 at 0:35
  • Uploading an image via Rich Media Share API is one step of the overall share sequence. The entire sequence is described in the Share on LinkedIn doc you've referenced. – identigral Jul 15 '19 at 1:57
  • When trying to upload rich media using the API it says I don't have permission to do it. Investigating a bit further I found out that I need permission directly from Linkedin to do this. I find this very weird as nowhere in the documentation is mentioned this. It says that I need the "w_member_social" to post on behalf of an authenticated member, which I do have. Therefore, I tried uploading the file as instructed in the Share on Linkedin doc but I got an error... – Zufra Jul 21 '19 at 15:18
  • ... saying the file was too big. So, I reduced the size of the image to half and I still get the same error which is quite baffling as the image is reasonably small. Any ideas? – Zufra Jul 21 '19 at 15:20
  • @Zufra As this issue isn't Salesforce specific, I recommend posting a new question with a trace of your request/response on StackOverflow rather than here – identigral Jul 21 '19 at 15:27
0

I was going to add a comment with a suggestion but it says it is too long. So my answer is not based on the fact that I got this working. It's simply an answer to your "what to put in the body of the 'POST' request mentioned in step 2" part. Will leave it to you to validate if it works.

I tried to find the HTTP request details from that curl example by appending the --trace-ascii /dev/stdout args. It gives me this request details (I used a smaller size image):

0000: PUT /mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?c
0040: a=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtB
0080: iZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-Ihpbf
00c0: XrRow1 HTTP/2
00cf: Host: api.linkedin.com
00e7: User-Agent: curl/7.54.0
0100: Accept: */*
010d: Authorization: Bearer redacted
012d: Content-Length: 28596

== Info: Connection state changed (MAX_CONCURRENT_STREAMS updated)!
=> Send data, 16384 bytes (0x4000)

0000: .PNG
0006: ......IHDR..............x......sBIT....|.d.....pHYs..........+..
0046: ....tEXtSoftware.www.inkscape.org..<... .IDATx...w.\.y7..s..>...
0086: #.*.It..#!......%ny.&q..I^G...M\b...0.cl.06H.M....$.z...j....{..
......

So the HTTP method that curl example uses is PUT. That body seems to be the string representation of the binary data with the first line .PNG denoting its file type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.