3

I'm working on making an Image Share with the LinkedIn v2 api. There are three steps, according to the LinkedIn docs.

  1. Register your image to be uploaded.
  2. Upload your image to LinkedIn.
  3. Create the image share.

I got past the first two using php curl, but I'm stuck on the third. Step 3, which is done with a ugcPost.

Here are is the post body I sent to the ugc endpoint:

{
    "author": "urn:li:person:{id-redacted}",
    "lifecycleState": "PUBLISHED",
    "specificContent": {
        "com.linkedin.ugc.ShareContent": {
            "shareCommentary": {
                "text": "hello"
            },
            "shareMediaCategory": "IMAGE",
            "media": {
                "stats": "READY",
                "description": {
                    "text": "this is a descriptoin"
                },
                "media": "urn:li:digitalmediaAsset:{id-redacted}",
                "title": {
                    "text": "this is some title text"
                }
            }
        }
    },
    "visibility": {
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
}

Here is the code i used to send curl post request with php:

    curl_setopt($ch, CURLOPT_URL, $ugcPostEndpoint);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, $out);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json","X-Restli-Protocol-Version: 2.0.0"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    $response = curl_exec($ch);
    $response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));

returns this from curl:

 Hostname was found in DNS cache
   Trying 108.174.10.12...
 Connected to api.linkedin.com (108.174.10.12) port 443 (#2)
 successfully set certificate verify locations:
   CAfile: none
  CApath: /etc/ssl/certs
 SSL connection using {redacted}
 Server certificate:
        subject: C=US; ST=California; L=Mountain View; O=LinkedIn Corporation; CN=tablet.linkedin.com
        start date: 2018-03-30 00:00:00 GMT
        expire date: 2020-04-27 12:00:00 GMT
        subjectAltName: api.linkedin.com matched
        issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
        SSL certificate verify ok.
 POST /v2/ugcPosts?oauth2_access_token={redacted} HTTP/1.1
Host: api.linkedin.com
Accept: */*
Content-Type:application/json
X-Restli-Protocol-Version: 2.0.0
Content-Length: 734

 upload completely sent off: 734 out of 734 bytes
< HTTP/1.1 500 Server Error
< X-LI-ResponseOrigin: RGW
< Content-Type: application/json
< X-RestLi-Error-Response: true
< X-Restli-Gateway-Error: false
< X-RestLi-Protocol-Version: 2.0.0
< X-Li-Fabric: prod-ltx1
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Li-Pop: prod-edc2
< X-LI-Proto: http/1.1
< X-LI-UUID: {redacted}
< Set-Cookie: {redacted}
< X-LI-Route-Key: {redacted}
<
 Connection #2 to host api.linkedin.com left intact

Why is this 500 error occuring? As far as I know I am following all of the examples/docs. (I know that 500 errors could be a problem on their end but in my experience when dealing with APIs 500 errors often mean that the curl request is wrong somehow.

James
  • 103
  • 8

1 Answers1

1

I figured out what I was doing wrong in case anyone else has a similar issue.

The media part of the body has to be the 0th element in an array like this:

 "media": [
            {
                "status": "READY",
                "description": {
                    "text": "this is a description"
                },
                "media": "urn:li:digitalmediaAsset:{id}",
                "title": {
                    "text": "this is some title text"
                }
            }
        ]

Not this:

     "media":
            {
                "status": "READY",
                "description": {
                    "text": "this is a description"
                },
                "media": "urn:li:digitalmediaAsset:{id}",
                "title": {
                    "text": "this is some title text"
                }
            }

The LinkedIn docs don't make this super obvious.

James
  • 103
  • 8