-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLinkedlnoAuthController.cls
190 lines (158 loc) · 10.3 KB
/
LinkedlnoAuthController.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
public class LinkedlnoAuthController{
public Boolean isCode { get; set; }
public String authCodeFromURL { get; set; }
public List<LinkedIn_Information__c> linkedlnInfoList { get; set; }
public String BasicInfo { get; set; }
public String urlToSharedPost { get; set; }
public String sharedInformation { get; set; }
public Final String consumerKey = 'XXXXXXXXXXXXXXXXXXXX'; // Your Client Id here
public Final String consumerSecret ='XXXXXXXXXXXXX'; // Your Client Secret here
public Final String redirect_URI = 'https://dreamhouse-a-dev-ed--c.ap5.visual.force.com/apex/LinkedlnoAuth'; // VF page preview URL
public String linkedln_Scope = 'r_fullprofile%20r_emailaddress';
public Final String linkedln_AuthUrl = 'https://www.linkedin.com/oauth/v2/authorization?';
public Final String accessToken_Endpoint = 'https://www.linkedin.com/oauth/v2/accessToken';
public LinkedlnoAuthController(){
authCodeFromURL = ApexPages.currentPage().getParameters().get('code');
if(authCodeFromURL == null || authCodeFromURL == '')
isCode = true;
else
isCode = false;
linkedlnInfoList = [Select Id, Name,Access_Token__c,Expires_In_Seconds__c From LinkedIn_Information__c Where Name='LinkedlnInfo'];
}
public PageReference doGetAuthorizationCode(){
String requestoAuthURL = linkedln_AuthUrl + 'response_type=code&client_id='+consumerKey+'&redirect_uri='+redirect_URI+
'&state=12345567dggd';
PageReference pageRef = new PageReference(requestoAuthURL);
return pageRef;
}
public void doGetAccessToken(){
Http http = new Http();
HttpRequest httpReq = new HttpRequest();
HttpResponse httpRes = new HttpResponse();
String requestTokenBody = 'code='+authCodeFromURL+'&grant_type=authorization_code'+
'&client_id='+consumerKey+'&client_secret='+consumerSecret+
'&redirect_uri='+redirect_URI;
String errorMessage = '';
System.debug('#### authCodeFromURL '+authCodeFromURL);
System.debug('#### requestTokenBody '+requestTokenBody);
httpReq.setMethod('POST');
httpReq.setEndpoint(accessToken_Endpoint);
httpReq.setHeader('Content-Type', 'application/x-www-form-urlencoded');
httpReq.setBody(requestTokenBody);
try{
httpRes = http.send(httpReq);
if(httpRes.getStatusCode() == 200){
Map<String,object> TokenInfo = (Map<String,object>)JSON.deserializeUntyped(httpRes.getBody());
LinkedIn_Information__c linkedlnInfo = new LinkedIn_Information__c();
linkedlnInfo.Access_Token__c = String.valueOf(TokenInfo.get('access_token'));
linkedlnInfo.Expires_In_Seconds__c = Double.valueOf(TokenInfo.get('expires_in'));
linkedlnInfo.Name ='LinkedlnInfo';
if(linkedlnInfoList!=null && linkedlnInfoList.size() > 0){
linkedlnInfo.Id = linkedlnInfoList[0].Id;
}
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,
'Congrattzzzzz..... You have done successfull Integration with Linkedln'));
upsert linkedlnInfo;
}else{
errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
+'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}
}catch(System.Exception e){
System.debug('#### Exception Excuted '+e.getStackTraceString()+' '+e.getMessage());
if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){
errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->'
+' Remote Site Setting and add '+' '+ accessToken_Endpoint +' Endpoint';
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}else{
errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
+'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}
}
}
public void doFetchBasicInfo(){
String errorMessage ='';
Http http = new Http();
HttpRequest httpReq = new HttpRequest();
HttpResponse httpRes = new HttpResponse();
List<LinkedIn_Information__c> linkedlnInfoListNew = [Select Id, Name,Access_Token__c,Expires_In_Seconds__c From LinkedIn_Information__c Where Name='LinkedlnInfo'];
httpReq.SetMethod('GET');
httpReq.setEndpoint('https://api.linkedin.com/v1/people/~?format=json');
httpReq.setHeader('Authorization', 'Bearer '+linkedlnInfoListNew[0].Access_Token__c);
httpReq.setHeader('Content-Type', 'application/json');
try{
httpRes = http.send(httpReq);
if(httpRes.getStatusCode() == 200){
Map<String,object> TokenInfo = (Map<String,object>)JSON.deserializeUntyped(httpRes.getBody());
String firstName = String.valueOf(TokenInfo.get('firstName'));
String lastName = String.valueOf(TokenInfo.get('lastName'));
String headline = String.valueOf(TokenInfo.get('headline'));
BasicInfo = firstName +' ' + lastName +' '+headline;
}else{
errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
+'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}
}catch(System.Exception e){
System.debug('#### Exception Excuted '+e.getStackTraceString()+' '+e.getMessage());
if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){
errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->'
+' Remote Site Setting and add '+' '+ 'https://api.linkedin.com/ '+' Endpoint';
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}else{
errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
+'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}
}
}
public void doSharePostOnLinkedIn(){
String errorMessage ='';
Http http = new Http();
HttpRequest httpReq = new HttpRequest();
HttpResponse httpRes = new HttpResponse();
List<LinkedIn_Information__c> linkedlnInfoListNew = [Select Id, Name,Access_Token__c,Expires_In_Seconds__c From LinkedIn_Information__c Where Name='LinkedlnInfo'];
httpReq.SetMethod('POST');
httpReq.setEndpoint('https://api.linkedin.com/v1/people/~/shares?format=json');
httpReq.setHeader('Authorization', 'Bearer '+linkedlnInfoListNew[0].Access_Token__c);
httpReq.setHeader('Content-Type', 'application/json');
httpReq.setHeader('x-li-format' , 'json');
String requestBody ='{'+
'"comment": "Check out developer.linkedin.com!",'+
'"content": {'+
'"title": "LinkedIn Developers Resources",'+
'"description": "Leverage LinkedIn’s APIs to maximize engagement",'+
'"submitted-url": "https://developer.linkedin.com"'+
'},'+
'"visibility": {'+
'"code": "anyone"'+
'}'+
'}';
httpReq.setBody(requestBody);
try{
httpRes = http.send(httpReq);
if(httpRes.getStatusCode() == 200 || httpRes.getStatusCode() == 201){
sharedInformation = requestBody;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, 'Post has been shared successfully!!'));
Map<String,object> sharedPostInformaionMap = (Map<String,object>)JSON.deserializeUntyped(httpRes.getBody());
urlToSharedPost = 'https://'+(String)sharedPostInformaionMap.get('updateUrl');
}else{
errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
+'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}
}catch(System.Exception e){
System.debug('#### Exception Excuted '+e.getStackTraceString()+' '+e.getMessage());
if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){
errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->'
+' Remote Site Setting and add '+' '+ 'https://api.linkedin.com/ '+' Endpoint';
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}else{
errorMessage = 'Unexpected Error while communicating with LinkedIn API. '
+'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
}
}
}
}