Using LinkedIn Developer Apps to Share Information to Your Profile or Company Page

Experiment on Laravel 7 by Sharing Article Posts

2 years ago SETHA THAY 2766
Using LinkedIn Developer Apps to Share Information to Your Profile or Company Page

I have shared the HOW TO technique on "Using Telegram Bot to Share Information to Telegram Channel" in my previous post. It gains attention quite good of knowledge wanting to share information to social media. Therefore, in this post, I am going to guide you through step by step how to use the LinkedIn developer app as an agent to share your information not only to your Linkedin Profile but also your company page. Prerequisite, In order to follow this guide you must have a Linkedin account and a company page which is created from your account.

Step 1: Create an app

api-post-to-linkedin-page-linkedin-portal

  • Create an app by giving App Name, LinkedIn Page, Privacy Policy URL, and App Logo then check on "I have read and agree to these terms" Click "Creat app"

api-post-to-linkedin-page-linkedin-create-app

  • You will see your app created in the list, next you have to verify your app by click on the button verify

api-post-to-linkedin-page-linkedin-created-app-success

  • LinkedIn will generate a verification URL as shown below

api-post-to-linkedin-page-send-verify-app

  • Copy and patse this URL into your browser and then click verify to confirm the responsibility between your app and your company page

api-post-to-linkedin-page-accept-verify-app

Step 2: Select product to use in your app (Product is referred to access permission LinkedIn will provide you to access its resources)

  • There are 3 products usually listed for you to select
  1. Sign In with LinkedIn used to enable existing LinkedIn user login to your website or website without signup and this will lead to permission access of OAuth2.0 with scopes of r_emailaddress and r_liteprofile
  2. Share on LinkedIn used to share posts, articles, images to the LinkedIn platform and this will lead to permission access of OAuth2.0 with scopes of r_member_social
  3. Marketing Developer Platform provides you to grow your business by building technology to reach, engage, and convert professional audiences at scale.

api-post-to-linkedin-page-send-product-to-review

For Sign In with LinkedIn and Share on Linkedin you can activate just about only 1mn but for Marketing Developer Platform you have to fill in a request form to LinkedIn support to enable this feature (I will show you how to fill in the request form in the below section). Once your product is accepted, you will see as below screen

api-post-to-linkedin-page-accepted-product

  • By enabling Sign In with LinkedIn and Share on LinkedIn products, you are able to use the LinkedIn API to receive profile information, create, modify and delete posts, comments, and reactions on your behalf. Still, you cannot post, modify, and delete posts, comments on behalf of your company page. You need to wait for acceptance from LinkedIn support for the product Marketing Developer Platform in order to do so. Below are the steps of the filling request form to submit to the LinkedIn support team

  • Given you name, your company name and address

api-post-to-linkedin-page-marketing-developer-platform-1

  • Given your client information

api-post-to-linkedin-page-marketing-developer-platform-2

  • Given your business information

api-post-to-linkedin-page-marketing-developer-platform-3

api-post-to-linkedin-page-marketing-developer-platform-4

  • Submit and waiting for approval, it might take a few days for LinkedIn to review, and some applications might be rejected based on some reason and you have to properly resubmit again :)

api-post-to-linkedin-page-marketing-developer-platform-5

api-post-to-linkedin-page-marketing-developer-platform-6

Step 3: Getting Access Token using Postman

  • Adding authorized redirect URLs for your app in auth tab of your app, and you also see the OAuth2.0 scope as well

https://oauth.pstmn.io/v1/callback

api-post-to-linkedin-page-getting-access-token-by-postman

  • Now let open your postman and fill in some info for getting an access token and this access token will expire in 2months. This means that every 2months you have to get a new access token, that's pretty inconvenient but we have no choice to follow because this is the setup from LinkedIn
  1. Select OAuth 2.0
  2. Grant Type: Authorization Code
  3. Callback URL: https://oauth.pstmn.io/v1/callback
  4. Check on Authorize using browser
  5. Auth URL: https://www.linkedin.com/oauth/v2/authorization
  6. Access Token URL: https://www.linkedin.com/oauth/v2/accessToken 
  7. Client ID: xxxxxxxxxxx
  8. Client Secret: xxxxxxxxxxxxxxxxxxxx
  9. Scope: w_member_social,r_liteprofile,r_emailaddress
  10. Client Authentication: Send client credentials in body

api-post-to-linkedin-page-getting-my-profile

  • Click the button Get New Access Token, the app will ask your consent to access information within the scope

api-post-to-linkedin-page-asking-for-consent-to-crud-post

  • After clicking allow, you will be authorized and got a new access token

api-post-to-linkedin-page-success-authorized

api-post-to-linkedin-page-success-got-token

Step 4: Access User Profile and Share an Article to User Profile in LinkedIn

  • Getting user profile by this API endpoint https://api.linkedin.com/v2/me by providing the below points in the header
    1. Authorization with the newly received access token
    2. X-Restli-Protocol-Version: 2.0.0

api-post-to-linkedin-page-getting-my-profile

  • Post an article to a LinkedIn profile with the following steps
    1. Using API endpoint https://api.linkedin.com/v2/ugcPosts
    2. Authorization with the newly received access token
    3. X-Restli-Protocol-Version: 2.0.0
    4. data to send to LinkedIn in the body using JSON
{
    "author": "urn:li:person:IkF1yaPkv_",
    "lifecycleState": "PUBLISHED",
    "specificContent": {
        "com.linkedin.ugc.ShareContent": {
            "shareCommentary": {
                "text": "Telegram channel is another popular platform to connect with audiences which posts are controlled by channel owners. The Channel owner is the one responsible for providing knowledge, articles, a..."
            },
            "shareMediaCategory": "ARTICLE",
            "media": [
                {
                    "status": "READY",  
                    "description": {
                        "text": "Telegram channel is another popular platform to connect with audiences which posts are controlled by channel owners. The Channel owner is the one responsible for providing knowledge, articles, a..."
                    },
                    "originalUrl": "https://nestcode.co/en/blog/using-telegram-bot-to-share-information-to-telegram-channel",
                    "title": {
                        "text": "Using Telegram Bot to Share Information to Telegram Channel"
                    },
                    "thumbnails":[
                        {
                            "url" : "https://nestcode.co/blog_images/using-telegram-bot-to-share-information-to-telegram-channel-600x400.png"
                        }
                    ]
                }
            ]
        }
    },
    "visibility": {
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
}

api-post-to-linkedin-page-success-shared-using-api

  • Go to your profile on LinkedIn to check

api-post-to-linkedin-page-success-shared-using-api-3

Step 5: Coding in Laravel 7 to Post in Your LinkedIn Profile and Company Page

  • Create environment variable to store author id and access token
MY_URN=urn:li:person:your_linkedin_id_here
PAGE_URN=urn:li:organization:your_companypage_id_here
LINKEDIN_ACCESS_TOKEN=your_2months_access-token_here
  • Adding menu share to LinkedIn profile and page
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="{{url('/share/linkedin_profile') . '/' . $post->post_id . '?locale=' . app('request')->get('locale')}}"><i class="fa fa-linkedin-square" aria-hidden="true"></i> &nbsp; Linkedin (Profile)</a>
<a class="dropdown-item" href="{{url('/share/linkedin_page') . '/' . $post->post_id . '?locale=' . app('request')->get('locale')}}"><i class="fa fa-linkedin-square" aria-hidden="true"></i> &nbsp; Linkedin (Page)</a>
<a class="dropdown-item" href="{{url('/share/telegram') . '/' . $post->post_id . '?locale=' . app('request')->get('locale')}}"><i class="fa fa-telegram" aria-hidden="true"></i> &nbsp; Telegram</a>
</div>
  • Adding routes to handle the logic of sharing
Route::get('/share/linkedin_profile/{postid}', 'IndexController@shareLinkedInProfile')->middleware('auth');
Route::get('/share/linkedin_page/{postid}', 'IndexController@shareLinkedInPage')->middleware('auth');
  • Using GuzzleHttp to send a post request to LinkedIn API Server
public function shareLinkedInProfile($postid, Request $request){
        $post = BinshopsPostTranslation::where('post_id', $postid)->first();
        $author = env('MY_URN');
        $token = env('LINKEDIN_ACCESS_TOKEN');
        $description = strip_tags(mb_strimwidth($post->post_body_output(), 0, 200, "..."));
        $text = $post->title;
        $url =  $post->url($request->get('locale'));
        $thumnail = $post->image_url();
        
        $preparedData = [
            "author" => $author,
            "lifecycleState" => "PUBLISHED",
            "specificContent" => [
                "com.linkedin.ugc.ShareContent" => [
                    "shareCommentary" => [
                        "text" => $description
                    ],
                    "shareMediaCategory" =>  "ARTICLE",
                    "media" => [
                        [
                            "status" => "READY",
                            "description" =>[
                                "text" => $description
                            ],
                            "originalUrl" => $url,
                            "title" => [
                                "text" => $text
                            ],
                            "thumbnails" => [
                                [
                                    "url" => $thumnail
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            "visibility" => [
                "com.linkedin.ugc.MemberNetworkVisibility" => "PUBLIC"
            ]
        ];
        $data = \json_encode($preparedData);
        $client = new \GuzzleHttp\Client();
        $client->request('POST', 'https://api.linkedin.com/v2/ugcPosts', [
            'headers' => [
                'X-Restli-Protocol-Version' => '2.0.0',
                'Authorization' => 'Bearer ' . $token,
                'Content-Type' => 'application/json'
            ],
            'body' => $data
        ]);
        return redirect()->back();
    }


THANK YOU AND HOPE YOU ENJOY THIS POST

Find Us @
Facebook
Telegram
Twitter
LinkedIn


About author

Author Profile

SETHA THAY

Software Engineer & Project Manager. I am willing to share IT knowledge, technical experiences and investment to financial freedom. Feel free to ask and contact me.



Scroll to Top