How to publish a Linkedin post containing video via API with Laravel Http Client

Intro

This article is about publishing a Linkedin post that contains video via Linkedin API call using Laravel Http Client. Even though the process is straightforward, few bumps on the road took time to figure everything out.

This article does not deal with obtaining the Linkedin token and person (read-only) or Organization Urn.

Three API calls

Three API calls are needed if you wish to publish the post with video, not counting API calls for obtaining token and URN.

  1. Register an upload for the video
  2. Upload video
  3. Publish post

Register video upload

The first step is to register an upload for the video.

public function registerUpload()
{
    $videoUploadAttributes =  [
        "registerUploadRequest" => [
            "owner" => "urn:li:organization:".self::SOCIAL_ID,
            "recipes" => [
                "urn:li:digitalmediaRecipe:feedshare-video"
            ],
            "serviceRelationships" => [
                [
                    "identifier" => "urn:li:userGeneratedContent",
                    "relationshipType" => "OWNER"
                ]
            ]
        ]
    ];

    return Http::withToken(self::TOKEN)->withHeaders([
        "Content-Type"  => "application/json",
        "x-li-format"   => "json"
    ])->post(self::BASE_URL.'assets?action=registerUpload', $videoUploadAttributes);
}

The constant SOCIAL_ID is the person or organisation URN number.

Here you can read the docs on how to register video upload.

Upload video

If everything goes as planned, the next step is to obtain the response's video upload URL and URN from the API response. Then you can upload the video.

public function uploadVideo($videoUrl, $videoUploadUrl)
{
    return Http::withToken(self::TOKEN)->timeout(self::TIMEOUT)
        ->withBody(file_get_contents($videoUrl), "application/octet-    stream")->put($videoUploadUrl);
}

Here are the relevant docs.

Publishing post

The final step is publishing the post.

public function publishPost($postContent, $videoURN)
{
    $ugcPostAttributes = [
        'author' => "urn:li:organization:".self::SOCIAL_ID,
        "lifecycleState" => "PUBLISHED",
        "specificContent" =>  [
            "com.linkedin.ugc.ShareContent" => [
                "shareCommentary" => [
                    "text" => $postContent
                ],
                "shareMediaCategory" => "VIDEO",
                "media" =>  [
                    [
                        "media" => $videoURN,
                        "status" => "READY",
                    ]
                ]
            ]
        ],
        "visibility" => [
            "com.linkedin.ugc.MemberNetworkVisibility" => "PUBLIC"
        ]
    ];

    return Http::withToken(self::TOKEN)->post(self::BASE_URL.'ugcPosts', $ugcPostAttributes);
}

Here are the relevant docs.

See it in action

I created the GitHub gist with Laravel command for publishing posts. Here you can see all the code needed.

https://gist.github.com/jcergolj/01c2a7965a01544dac2adb1673ab86e2

Before you can run the command, you should add SOCIAL_ID and TOKEN.

php artisan linkedin:publish-post "post content" https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4

Conclusion

Hopefully, you'll find this article insightful and that you'll spend less time than me figure it out how to publish a Linkedin post with a video using Laravel Http Client. If you have any questions or issues, feel free to reach out to me on Twitter.

Relevant links

UGC post API

Vector asset API

Laravel http client docs

My gist