Hello Muleys,
This is my second blog post for Mulesoft and in this blog post, I am going to talk how you can connect Your LinkedIn account with Mulesoft and do the Post from the same.
Test before implementation
If you want to see how it will work Hit the below URL from the browser and do not worry this is safe with any kind of attack. If the Link is not working then jump to the implementation part
End Result
- Make a Post over LinkedIn
- Comment on the Same Post
- Like the same post
Below is the sample post which we are going to post using this Mulesoft application
Connector Required
- HTTP Listener
- HTTP Requester
- Flow Reference
- Set Variable
- Transform Message
Let’s start implementation
Step1 – Create a Connected application in LinkedIn
Got to https://www.linkedin.com/developers/ and click on Create App
fill all the required field and the click on create app.
Now, once the application is created then click on Auth Tab and note down the client Id and client secret. And click on Edit ( pencil icon ) under OAuth 2.0 settings.
Click on Add Redirect URL
Add any dummy redirect URL and we will update this later and then click update
Now, Click on Products Tab and select the product select on the below screenshot. Products are required in order to make a post over linkedIn and get the access token
Step 2 – Create the Mulesoft Application
Open your Mulesoft studio and create a new Mule project and once the project is created then drag and drop HTTP Listener to canvas plane and click on the + icon next to connector configuration. Provide the path that you wanted to provide.
Provide the port no for your connector configuration. So your url be like below
https://localhost:8081/linkedIn
If you have provided the other port and path then make the changes into the above URL and then add the same URL as the call back URL in the LinkedIn that we said that we will change later on.
Hit the below URL from the browser and replace the client Id with your client Id and redirect the URL with your URL value.
Step 4 – Get the LinkedIn Access Token
We will be hitting https://www.linkedin.com/oauth/v2/accessToken URL with the below body to get the access token
Body – In the body replace client Id and client secret value with your values and also replace the redirect URL with your URL.
grant_type=authorization_code&code={authorization_code_from_step2_response}&redirect_uri=hhttps%3A%2F%2Fdev.example.com%2Fauth%2Flinkedin%2Fcallback&client_id={your_client_id}&client_secret={your_client_secret}
Headers
- Host: www.linkedin.com
- Content-Type: application/x-www-form-urlencoded
Step 5 – Get LinkedIn user information
Hit “https://api.linkedin.com/v2/me” url from the HTTP Request and pass the below header
Method : GET
- Content-Type : application/json
- Authorization : Bearer “Access Token from the previous step”
Step 6 – Make a post into LinkedIn
We will use share API for making a post into the LinkedIn and here is the code which we have used from Mulesoft
Hit “https://api.linkedin.com/v2/shares” URL from the HTTP Request with POST Method and use below headers
- Content-Type : application/json
- Authorization : Bearer “Access Token from the previous step”
As this is a post method we need to pass the body which is given below
{
"content": {
"contentEntities": [
{
"entityLocation": "https://sravanlingam.wordpress.com/2020/04/02/how-to-build-a-custom-login-page-using-mulesoft/amp/",
"thumbnails": [
{
"resolvedUrl": "https://sravanlingam.files.wordpress.com/2020/04/0.png"
}
]
}
],
"title": "Custom Login Page using Mulesoft"
},
"owner": "urn:li:person:" ++ vars.UserInfo.urn,
"subject": "Custom Login Page using Mulesoft",
"text": {
"text": "Dear Muleys,
Learn how to create a Custom Login Page using Mulesoft by Sravan Lingam. Post created by "
++ vars.UserInfo.firstName ++ " " ++ vars.UserInfo.lastName ++
" Application Developed By Amit Singh aka SFDCPanther MuleSoft helps us to Integrate things in a seamless way! #MuleSoft #mule4 #integration #linkedin #mulesoftdevelopers "
}
}
Get the code for the complete application below
<?xml version="1.0" encoding="UTF-8"?>
<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="765c592c-45b5-4531-9829-0b783024853c" basePath="/panther" >
<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="cc7d2823-544b-4a0b-880a-f76c57689aee" >
<http:request-connection protocol="HTTPS" host="www.linkedin.com" />
</http:request-config>
<http:request-config name="HTTP_Request_configuration1" doc:name="HTTP Request configuration" doc:id="bdf44d24-9779-4a90-9f8f-344b8760857f" />
<flow name="linkedin-mulesoftFlow" doc:id="a9b992b3-fccb-42bd-8877-24c039f59cb1" >
<http:listener doc:name="GET:/Code" doc:id="ea69868a-86ca-4eed-8046-f8645a5d6b75" config-ref="HTTP_Listener_config" path="/linkedIn"/>
<set-variable value='#[(attributes.queryParams.code default "" ) as String]' doc:name="LinkedIn Code" doc:id="59e1ea74-d585-4dfb-9ce4-431b01067a71" variableName="code"/>
<http:request method="POST" doc:name="POST :/oauth/v2/accessToken" doc:id="a816b55c-27e7-4ff4-9554-6c21906b97b1" config-ref="HTTP_Request_configuration" path="/oauth/v2/accessToken">
<http:body ><![CDATA[#["grant_type=authorization_code&code=" ++(vars.code default "" ) as String ++ "&redirect_uri=http://linkedin-mulesoft.us-e2.cloudhub.io/panther/linkedIn&client_id=78uzxjnt2stq6x&client_secret=78bv3emsNWnh0RUM"]]]></http:body>
<http:headers ><![CDATA[#[output application/java
---
{
"Host" : "www.linkedin.com",
"Content-Type" : "application/x-www-form-urlencoded"
}]]]></http:headers>
</http:request>
<ee:transform doc:name="Parse Token" doc:id="1e40af02-ffd4-4906-aea1-01acd4f1674b" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload]]></ee:set-payload>
</ee:message>
</ee:transform>
<set-variable value="#[payload.access_token]" doc:name="token" doc:id="19d5af97-18e8-469e-b2d7-c37fce50f7c4" variableName="token"/>
<flow-ref doc:name="Flow Reference" doc:id="82d58aea-53ca-4ccd-bbfb-afa889593116" name="GetUserDetails"/>
</flow>
<flow name="GetUserDetails" doc:id="ded4b81e-14cc-47bc-b78c-14591dca01c2" >
<http:request method="GET" doc:name="GET :/v2/me" doc:id="f2aa5443-eedd-4788-951c-e02d052d09c1" url='#["https://api.linkedin.com/v2/me"]'>
<http:headers ><![CDATA[#[output application/json
---
{
"Authorization" : "Bearer " ++ (vars.token default "") as String,
"Content-Type" : "application/json"
}]]]></http:headers>
</http:request>
<set-variable value="#[output application/json --- { urn : payload.id, firstName : payload.localizedFirstName, lastName : payload.localizedLastName }]" doc:name="User Information" doc:id="328d8c2a-787d-486e-8dbf-c537d225dedf" variableName="UserInfo"/>
<http:request method="POST" doc:name="POST :/v2/shares" doc:id="76d00a59-a28c-4edd-b9da-ee31a8e8cde2" url='#["https://api.linkedin.com/v2/shares"]'>
<http:body ><![CDATA[#[{
"content": {
"contentEntities": [
{
"entityLocation": "https://sravanlingam.wordpress.com/2020/04/02/how-to-build-a-custom-login-page-using-mulesoft/amp/",
"thumbnails": [
{
"resolvedUrl": "https://sravanlingam.files.wordpress.com/2020/04/0.png"
}
]
}
],
"title": "Custom Login Page using Mulesoft"
},
"owner": "urn:li:person:" ++ vars.UserInfo.urn,
"subject": "Custom Login Page using Mulesoft",
"text": {
"text": "Dear Muleys,
Learn how to create a Custom Login Page using Mulesoft by Sravan Lingam. Post created by "
++ vars.UserInfo.firstName ++ " " ++ vars.UserInfo.lastName ++
" Application Developed By Amit Singh aka SFDCPanther MuleSoft helps us to Integrate things in a seamless way! #MuleSoft #mule4 #integration #linkedin #mulesoftdevelopers "
}
}]]]></http:body>
<http:headers ><![CDATA[#[output application/json
---
{
"Authorization" : "Bearer " ++ (vars.token default "") as String,
"Content-Type" : "application/json"
}]]]></http:headers>
</http:request>
<set-variable value="#[{ shareUrn : payload.activity }]" doc:name="Share Information" doc:id="92b3df8b-5fea-4891-88e2-0c051ceceba8" variableName="share"/>
<http:request method="POST" doc:name="POST :/v2/socialActions/" doc:id="fd3cd71a-3876-417f-b27a-8b02ede54f5e" url='#["https://api.linkedin.com/v2/socialActions/" ++ vars.share.shareUrn ++"/comments"]'>
<http:body ><![CDATA[#[{
"actor": "urn:li:person:" ++ (vars.UserInfo.urn) as String,
"object": (vars.share.shareUrn default "") as String,
"message":{
"text":"This comment and Like on the post are created from Mulesoft Application"
}
}]]]></http:body>
<http:headers ><![CDATA[#[output application/json
---
{
"Authorization" : "Bearer " ++ (vars.token default "") as String,
"Content-Type" : "application/json"
}]]]></http:headers>
</http:request>
<http:request method="POST" doc:name="POST /v2/socialActions/likes" doc:id="81c84c36-bb7a-488e-91aa-986d32849bef" url='#["https://api.linkedin.com/v2/socialActions/" ++ vars.share.shareUrn ++"/likes"]'>
<http:body ><![CDATA[#[{
"actor": "urn:li:person:" ++ (vars.UserInfo.urn) as String,
"object": (vars.share.shareUrn default "") as String,
}]]]></http:body>
<http:headers ><![CDATA[#[output application/json
---
{
"Authorization" : "Bearer " ++ (vars.token default "") as String,
"Content-Type" : "application/json"
}]]]></http:headers>
</http:request>
<ee:transform doc:name="Transform Message" doc:id="2c696daa-1aa5-4de0-a698-4d31ddc690fe">
<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>
After you implemented then run your application and then hit the below url from the browser.
Before making the call to URL make sure you are using your own client Id and redirect URL.
And you will see the magic
Happy to help for any issues. #Mule4 #MuleDeveloper #Mulesoft
[…] your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Accept Read […]