{{announcement.body}}
{{announcement.title}}

Integration With Social Media Platforms Series (Part 1)

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

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"

mulesoft linkedin

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

https://www.linkedin.com/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 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

code value

  • 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.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=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

/v2/me

  • When you hit the above request, you will get the user info of the user who is requesting your app. See the sample response:
Java
 







  • 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
Java
 




xxxxxxxxxx
1
37


 
1
%dw 2.0
2
 
          
3
output application/json
4
 
          
5
---
6
 
          
7
{
8
 
          
9
    "author": "urn:li:person:" ++ vars.UserInfo.urn,
10
 
          
11
    "lifecycleState": "PUBLISHED",
12
 
          
13
    "specificContent": {
14
 
          
15
        "com.linkedin.ugc.ShareContent": {
16
 
          
17
            "shareCommentary": {
18
 
          
19
                "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. 
20
 
          
21
                MuleSoft helps us to Integrate things in a seamless way! #MuleSoft #mule4 #integration #linkedin #mulesoftdevelopers "
22
 
          
23
            },
24
 
          
25
            "shareMediaCategory": "NONE"
26
 
          
27
        }
28
 
          
29
    },
30
 
          
31
    "visibility": {
32
 
          
33
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
34
 
          
35
    }
36
 
          
37
}



  • Now call the final request with below config

API linkedin

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

XML
 




xxxxxxxxxx
1
150


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
 
          
3
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
4
 
          
5
    xmlns="http://www.mulesoft.org/schema/mule/core"
6
 
          
7
    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
8
 
          
9
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
10
 
          
11
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
12
 
          
13
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="7d359058-996f-4c6d-a5aa-ddb46d09f5c3" >
14
 
          
15
        <http:listener-connection host="0.0.0.0" port="8081" />
16
 
          
17
    </http:listener-config>
18
 
          
19
    <http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="c3763ec9-4faf-4662-880e-ddb96bb56a46" />
20
 
          
21
    <http:request-config name="HTTP_Request_configuration1" doc:name="HTTP Request configuration" doc:id="d613ec93-39fa-451d-85d1-b946a8bf3f6f" />
22
 
          
23
     
24
 
          
25
    <flow name="linkedin-integrationFlow" doc:id="440c552a-daef-47df-910d-174c36446d94" >
26
 
          
27
        <http:listener doc:name="/linkedin/callback" doc:id="40aae80c-370b-4938-9fb8-7fe4af566113" config-ref="HTTP_Listener_config" path="/linkedin/callback"/>
28
 
          
29
        <set-variable value="#[attributes.queryParams.code]" doc:name="code value from step1" doc:id="416bd56a-6b98-4c22-9b55-8d27f627f4f3" variableName="code"/>
30
 
          
31
        <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 ++
32
 
          
33
"&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">
34
 
          
35
        </http:request>
36
 
          
37
        <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">
38
 
          
39
            <http:headers ><![CDATA[#[output application/java
40
 
          
41
---
42
 
          
43
{
44
 
          
45
    Authorization : "Bearer " ++ vars.accessToken.access_token as String
46
 
          
47
    
48
 
          
49
}]]]></http:headers>
50
 
          
51
        </http:request>
52
 
          
53
        <set-variable value="#[output application/json
54
 
          
55
---
56
 
          
57
{
58
 
          
59
urn :    payload.id,
60
 
          
61
firstName : payload.localizedFirstName,
62
 
          
63
lastName : payload.localizedLastName
64
 
          
65
}]" doc:name="UserInfo" doc:id="c8f6ff8d-0170-4124-8a20-bba92968de83" variableName="UserInfo"/>
66
 
          
67
        <ee:transform doc:name="Post Body" doc:id="80ded147-02aa-4a8c-8412-a8a97f633515" >
68
 
          
69
            <ee:message >
70
 
          
71
                <ee:set-payload ><![CDATA[%dw 2.0
72
 
          
73
output application/json
74
 
          
75
---
76
 
          
77
{
78
 
          
79
    "author": "urn:li:person:" ++ vars.UserInfo.urn,
80
 
          
81
    "lifecycleState": "PUBLISHED",
82
 
          
83
    "specificContent": {
84
 
          
85
        "com.linkedin.ugc.ShareContent": {
86
 
          
87
            "shareCommentary": {
88
 
          
89
                "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. 
90
 
          
91
                MuleSoft helps us to Integrate things in a seamless way! #MuleSoft #mule4 #integration #linkedin #mulesoftdevelopers "
92
 
          
93
            },
94
 
          
95
            "shareMediaCategory": "NONE"
96
 
          
97
        }
98
 
          
99
    },
100
 
          
101
    "visibility": {
102
 
          
103
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
104
 
          
105
    }
106
 
          
107
}]]></ee:set-payload>
108
 
          
109
            </ee:message>
110
 
          
111
        </ee:transform>
112
 
          
113
        <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" >
114
 
          
115
            <http:headers ><![CDATA[#[output application/java
116
 
          
117
---
118
 
          
119
{
120
 
          
121
    Authorization : "Bearer " ++ vars.accessToken.access_token as String
122
 
          
123
    
124
 
          
125
}]]]></http:headers>
126
 
          
127
        </http:request>
128
 
          
129
        <ee:transform doc:name="Transform Message" doc:id="db9805ec-cc12-4693-b644-77dd7c3de801" >
130
 
          
131
            <ee:message >
132
 
          
133
                <ee:set-payload ><![CDATA[%dw 2.0
134
 
          
135
output application/json
136
 
          
137
---
138
 
          
139
"Your post Was Successfully Posted!"]]></ee:set-payload>
140
 
          
141
            </ee:message>
142
 
          
143
        </ee:transform>
144
 
          
145
    </flow>
146
 
          
147
</mule>



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

Sravan Lingam

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

Topics:
integration, mule 4, mule 4 tutorial, mulesoft, mulesoft 4, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}