Integration with Social Media Platforms  Series - Part 1-Integrating LinkedIn with MuleSoft - Share a Post via MuleSoft Built REST API

Integration with Social Media Platforms Series - Part 1-Integrating LinkedIn with MuleSoft - Share a Post via MuleSoft Built REST API

Hey Guys,

It's always fun to learn something interesting!

I am going to start a series of Articles on Integrating MuleSoft with the Social Media Platform.

This article helps you to build a RESTful api through MuleSoft that integrates with LinkedIn and shares a post on behalf of one's personal account.

Pre-requisites :

Create a Developer account at LinkedIn https://developer.linkedin.com/

Follow the steps till verifying the page as shown in the below video:

3 Steps to Integrate :

This article shows you how to Integrate locally. That's the reason I have used the Callback URL as localhost. Once your testing is done, you can deploy your app on cloudhub and edit the callback URL in developer.linkedin.com in your app settings.

Step 1:

Go to your app in developer.linkedin.com, and add the products "Share" and "Sign-in"

No alt text provided for this image

Once done. Click on read docs : https://docs.microsoft.com/en-gb/linkedin/consumer/integrations/self-serve/share-on-linkedin

You can see how the OAuth authentication process is followed. For now, I'll show you how to implement it. We do not require an OAuth account. LinkedIn will take care of it!

Step 2:

Create a new Mule Project in Anypoint Studio, have a listener where the path is equal to the path that we provided in the callback URL in the developer portal. in our case its

https://localhost:8081/linkedin/callback

Now we shall start implementing the flow. Its very simple. Follow the process.

Your callback URL is triggered when you hit the below mentioned URL first.

Try to frame this URL properly. Before framing, Goto https://www.urlencoder.org/

encode your callback URL. Because we are going to pass this callback URL as a query param

Original callback: https://localhost:8081/linkedin/callback

encoded callback : http%3A%2F%2Flocalhost%3A8081%2Flinkedin%2Fcallback

Note: When you use cloudhub URL (once the app deployed on cloudhub) make sure you are giving the encoded cloudhub callback URL.

Now keep things handy with values of Client ID,Client Secret that we have in the app we created in developer.linkedin.com

Now Main URL :

https://www.dhirubhai.net/oauth/v2/authorization?response_type=code&client_id=658dbkbjks6&redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Flinkedin%2Fcallback&state=fooobar&scope=r_liteprofile%20r_emailaddress%20w_member_social

You can see, we are passing response_type,client_id,redirect_uri,state,scope as query params. Do provide values for the same. client_id is your client id, redirect URI is encoded callback URL, state value can be set to anything, the scope must be provided with the same values which I provided in above URL

Once Main URL is triggered, it will call our callback URL . now we receive a request from main URL which has a code value.

The flow is as followed

No alt text provided for this image


  • Store the code value in a variable with value attributes.queryParams.code
  • call an external URL to get access token : Place http Requestor and give below URL in "URL" path enabling expression:
"https://www.dhirubhai.net/oauth/v2/accessToken?code="++ vars.code as String ++"&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Flinkedin%2Fcallback&client_secret=dMerdsq8MBmFQiQbB&client_id=658dbkbjks6"
  • Once you hit this request, we will receive an access token. Now save this token in a variable.
  • Now call another request with below configuration
No alt text provided for this image
  • When you hit the above request, you will get the user info of the user who is requesting your app. See the sample response:
{

    "localizedLastName": " ????????????",

    "profilePicture": {

        "displayImage": "urn:li:digitalmediaAsset:C4D0fcsGOmgze2Rq01w"

    },

    "firstName": {

        "localized": {

            "en_US": "???????????? "

        },

        "preferredLocale": {

            "country": "US",

            "language": "en"

        }

    },

    "lastName": {

        "localized": {

            "en_US": " ????????????"

        },

        "preferredLocale": {

            "country": "US",

            "language": "en"

        }

    },

    "id": “hydsc3FrRVi", //urn id
 
  • So you can make use of these details further. id field is important which is the urn id of that profile.
  • Now we are one step away to post via Linkedin.
  • Frame the request in a Transform message as below
%dw 2.0

output application/json

---

{

    "author": "urn:li:person:" ++ vars.UserInfo.urn,

    "lifecycleState": "PUBLISHED",

    "specificContent": {

        "com.linkedin.ugc.ShareContent": {

            "shareCommentary": {

                "text": "Hello Mule World! I am " ++ vars.UserInfo.firstName ++ " " ++ vars.UserInfo.lastName ++ " . This is my first Share on LinkedIn via MuleSoft built RESTAPI by Sravan Lingam! Who created a sample application to post on LinkedIn. 

                MuleSoft helps us to Integrate things in a seamless way! #MuleSoft #mule4 #integration #linkedin #mulesoftdevelopers "

            },

            "shareMediaCategory": "NONE"

        }

    },

    "visibility": {

        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"

    }

}
  • Now call the final request with below config
No alt text provided for this image

That's it we are all Good!. Now you can test the app using the main URL that we have given in Step2 .

Now you can see your app will request the user to Allow access, once the user clicks on "Allow" Your post will be posted :)

Simple. Once you test this locally, you can deploy your mule app in Cloudhub and then update your call back URL in the developer account.

Note:

DO encode the url whenever you make changes.

PFB code :

<?xml version="1.0" encoding="UTF-8"?>




<mule xmlns:ee="https://www.mulesoft.org/schema/mule/ee/core" xmlns:http="https://www.mulesoft.org/schema/mule/http"

	xmlns="https://www.mulesoft.org/schema/mule/core"

	xmlns:doc="https://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.mulesoft.org/schema/mule/core https://www.mulesoft.org/schema/mule/core/current/mule.xsd

https://www.mulesoft.org/schema/mule/http https://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

https://www.mulesoft.org/schema/mule/ee/core https://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="7d359058-996f-4c6d-a5aa-ddb46d09f5c3" >

		<http:listener-connection host="0.0.0.0" port="8081" />

	</http:listener-config>

	<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="c3763ec9-4faf-4662-880e-ddb96bb56a46" />

	<http:request-config name="HTTP_Request_configuration1" doc:name="HTTP Request configuration" doc:id="d613ec93-39fa-451d-85d1-b946a8bf3f6f" />

	 

	<flow name="linkedin-integrationFlow" doc:id="440c552a-daef-47df-910d-174c36446d94" >

		<http:listener doc:name="/linkedin/callback" doc:id="40aae80c-370b-4938-9fb8-7fe4af566113" config-ref="HTTP_Listener_config" path="/linkedin/callback"/>

		<set-variable value="#[attributes.queryParams.code]" doc:name="code value from step1" doc:id="416bd56a-6b98-4c22-9b55-8d27f627f4f3" variableName="code"/>

		<http:request method="GET" doc:name="/accessToken" doc:id="9e2cea6c-afd6-4d32-8b58-0f106dd83dec" config-ref="HTTP_Request_configuration" url='#["https://www.dhirubhai.net/oauth/v2/accessToken?code="++ vars.code as String ++

"&amp;grant_type=authorization_code&amp;redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Flinkedin%2Fcallback&amp;client_secret=dMiaYq8MBmFQiQbB&amp;client_id=77osbgc6injdi6"]' target="accessToken">

		</http:request>

		<http:request method="GET" doc:name="/v2/me" doc:id="0027cae4-2fba-4e6b-9d28-7789f8a30b54" config-ref="HTTP_Request_configuration" url="https://api.linkedin.com/v2/me">

			<http:headers ><![CDATA[#[output application/java

---

{

	Authorization : "Bearer " ++ vars.accessToken.access_token as String

	

}]]]></http:headers>

		</http:request>

		<set-variable value="#[output application/json

---

{

urn :	payload.id,

firstName : payload.localizedFirstName,

lastName : payload.localizedLastName

}]" doc:name="UserInfo" doc:id="c8f6ff8d-0170-4124-8a20-bba92968de83" variableName="UserInfo"/>

		<ee:transform doc:name="Post Body" doc:id="80ded147-02aa-4a8c-8412-a8a97f633515" >

			<ee:message >

				<ee:set-payload ><![CDATA[%dw 2.0

output application/json

---

{

    "author": "urn:li:person:" ++ vars.UserInfo.urn,

    "lifecycleState": "PUBLISHED",

    "specificContent": {

        "com.linkedin.ugc.ShareContent": {

            "shareCommentary": {

                "text": "Hello Mule World! I am " ++ vars.UserInfo.firstName ++ " " ++ vars.UserInfo.lastName ++ " . This is my first Share on LinkedIn via MuleSoft built RESTAPI by Sravan Lingam! Who created a sample application to post on LinkedIn. 

                MuleSoft helps us to Integrate things in a seamless way! #MuleSoft #mule4 #integration #linkedin #mulesoftdevelopers "

            },

            "shareMediaCategory": "NONE"

        }

    },

    "visibility": {

        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"

    }

}]]></ee:set-payload>

			</ee:message>

		</ee:transform>

		<http:request method="POST" doc:name="/v2/ugcPosts" doc:id="f0ae6779-a2d8-4c38-9047-8b9147ab6ff7" config-ref="HTTP_Request_configuration" url="https://api.linkedin.com/v2/ugcPosts" >

			<http:headers ><![CDATA[#[output application/java

---

{

	Authorization : "Bearer " ++ vars.accessToken.access_token as String

	

}]]]></http:headers>

		</http:request>

		<ee:transform doc:name="Transform Message" doc:id="db9805ec-cc12-4693-b644-77dd7c3de801" >

			<ee:message >

				<ee:set-payload ><![CDATA[%dw 2.0

output application/json

---

"Your post Was Successfully Posted!"]]></ee:set-payload>

			</ee:message>

		</ee:transform>

	</flow>

</mule>

You can see the below post is posted on behalf of your app:

No alt text provided for this image


You can always feel free to reach out to me if you have any queries while implementing this codebase.


Happy Learning ,
Yours
Sravan Lingam







Iyyappan Mani

11x Certified Salesforce Professional at Logitech || PSM II | Volunteer || Clicks??|| Mentor

4 年

Step 2: Create a new Mule Project in Anypoint Studio... (after this dont know what to do) Could you please explain bit detail with screenshot

回复
Oscar Parra

Solutions Engineering @ Odaseva | ?? Data Security Platform for Salesforce

4 年

Woo, thanks Sravan Lingam !! Thank you for sharing the insight and excited to learn more about MuleSoft software and their API integrations. Could you link some resources (based on your experience) for an engineer (already familiar with API and building REST APIs) but something more on the architecture, etc ?

Sravan Lingam

MuleSoft Ambassador ???? Solution Architect ?? Mulesoft Meetup Leader ??All Certified MuleSoft Dev & Architect | 3 times MuleSoft All Star ??Award winner ???Owner of MuleSoft TechZone

4 年
Rajat v

Mulesoft Solution Architect| MCIA|MCPA|MCDL1|MIA

4 年

Thanks Sravan Lingam for sharing this??

John Mathew Philip

Senior Integration Consultant at NJC Labs | MuleSoft Mentor | MuleSoft Meetup Leader - Calicut Group | 5x MuleSoft Certified

4 年

Thanks Sravan Lingam was waiting for this.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了