Integration With Social Media Platforms Series (Part 1)
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.
Join the DZone community and get the full member experience.
Join For FreeHey 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"
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
http://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: http://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
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 the 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
- 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:
- 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
- When you hit the above request, you will get the user info of the user who is requesting your app. See the sample response:
xxxxxxxxxx
{
"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
xxxxxxxxxx
%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
That's it we are all Good!. Now you can test the app using the main URL that we have given in Step 2.
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
xxxxxxxxxx
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://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.linkedin.com/oauth/v2/accessToken?code="++ vars.code as String ++
"&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Flinkedin%2Fcallback&client_secret=dMiaYq8MBmFQiQbB&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:
You can always feel free to reach out to me if you have any queries while implementing this codebase.
Opinions expressed by DZone contributors are their own.
Comments