-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
package com.sm.config.social; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.jayway.jsonpath.DocumentContext; | ||
import com.jayway.jsonpath.JsonPath; | ||
import com.sm.common.error.type.ErrorType; | ||
import com.sm.common.exception.GenericErrorException; | ||
import com.sm.common.util.DateUtil; | ||
import com.sm.common.util.SecureStringUtil; | ||
import com.sm.config.social.ios.IOSClient; | ||
import com.sm.config.social.user.IOSInfo; | ||
import com.sm.config.social.user.FacebookUser; | ||
import com.sm.config.social.user.GoogleUser; | ||
import com.sm.config.social.user.IOSInfo; | ||
import com.sm.config.social.user.LinkedinInfo; | ||
import com.sm.controller.dto.request.SocialRequestModel; | ||
import com.sm.dto.response.user.UserDto; | ||
import org.json.JSONObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
|
@@ -113,6 +122,54 @@ public UserDto getAppleUser(SocialRequestModel socialRequestModel,String clientS | |
return userDto; | ||
} | ||
|
||
|
||
public UserDto getLinkedinInfo(String authorizationCode){ | ||
String redirectUrl = "http:https://localhost:8080/home"; | ||
String clientId = "abcdef"; | ||
String clientSecret = "helloworld"; | ||
//authorization code for access token | ||
String accessTokenUri ="https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code="+authorizationCode+"&redirect_uri="+redirectUrl+"&client_id="+clientId+"&client_secret="+clientSecret+""; | ||
String accessTokenRequest = restTemplate.getForObject(accessTokenUri, String.class); | ||
JSONObject jsonObjOfAccessToken = new JSONObject(accessTokenRequest); | ||
String accessToken = jsonObjOfAccessToken.get("access_token").toString(); | ||
|
||
//trade your access token | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Authorization", "Bearer " +accessToken); | ||
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); | ||
// linkedin api to get linkedidn profile detail | ||
String profileUri = "https://api.linkedin.com/v2/me"; | ||
ResponseEntity<String> linkedinDetailRequest = restTemplate.exchange(profileUri, HttpMethod.GET, entity, String.class); | ||
|
||
LinkedinInfo linkedinInfo = null; | ||
//response below for profile detail | ||
//{"localizedLastName":"world","lastName":{"localized":{"en_US":"world"},"preferredLocale":{"country":"US","language":"en"}},"firstName":{"localized":{"en_US":"hello"},"preferredLocale":{"country":"US","language":"en"}},"id":"abcdef","localizedFirstName":"hello"} | ||
|
||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
linkedinInfo = objectMapper.readValue(linkedinDetailRequest.getBody(), LinkedinInfo.class); | ||
} catch (Exception e) { | ||
logger.error("While fetching linkedin data, getting Exception :",e); | ||
throw new GenericErrorException(ErrorType.TOKEN_BAD_REQUEST.getCode(),ErrorType.TOKEN_BAD_REQUEST.getMessage(),e); | ||
} | ||
|
||
linkedinInfo = getLinkedinEmailInfo(entity, linkedinInfo); | ||
return convertTo(linkedinInfo); | ||
|
||
} | ||
|
||
private LinkedinInfo getLinkedinEmailInfo(HttpEntity<String> entity, LinkedinInfo linkedinInfo) { | ||
ResponseEntity<String> linkedinDetailRequest; | ||
String emailUri = "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))"; | ||
linkedinDetailRequest = restTemplate.exchange(emailUri, HttpMethod.GET, entity, String.class); | ||
|
||
//{"elements":[{"handle":"urn:li:emailAddress:1000001","handle~":{"emailAddress":"[email protected]"}}]} | ||
String ee = "$['elements'][0]['handle']['emailAddress']"; | ||
DocumentContext jsonContext = JsonPath.parse(linkedinDetailRequest.getBody()); | ||
linkedinInfo.setEmail(jsonContext.read("$['elements'][0]['handle~']['emailAddress']")); | ||
return linkedinInfo; | ||
} | ||
|
||
private UserDto convertTo(FacebookUser facebookUser) { | ||
UserDto userDto = new UserDto(); | ||
userDto.setProviderId(facebookUser.getId()); | ||
|
@@ -144,4 +201,15 @@ private UserDto convertTo(IOSInfo appleInfo) { | |
userDto.setPassword(SecureStringUtil.randomString(30)); | ||
return userDto; | ||
} | ||
|
||
private UserDto convertTo(LinkedinInfo linkedinInfo) { | ||
UserDto userDto = new UserDto(); | ||
userDto.setProviderId(linkedinInfo.getId()); | ||
userDto.setProvider("linkedin"); | ||
userDto.setEmail(linkedinInfo.getEmail()); | ||
userDto.setFirstName(linkedinInfo.getLocalizedFirstName()); | ||
userDto.setLastName(linkedinInfo.getLocalizedLastName()); | ||
userDto.setPassword(SecureStringUtil.randomString(30)); | ||
return userDto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.sm.config.social.user; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class LinkedinInfo { | ||
private String id; | ||
private String localizedFirstName; | ||
private String localizedLastName; | ||
private String email; | ||
|
||
public LinkedinInfo(){} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getLocalizedFirstName() { | ||
return localizedFirstName; | ||
} | ||
|
||
public void setLocalizedFirstName(String localizedFirstName) { | ||
this.localizedFirstName = localizedFirstName; | ||
} | ||
|
||
public String getLocalizedLastName() { | ||
return localizedLastName; | ||
} | ||
|
||
public void setLocalizedLastName(String localizedLastName) { | ||
this.localizedLastName = localizedLastName; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.sm.controller; | ||
|
||
import com.sm.common.exception.GenericErrorException; | ||
import io.swagger.annotations.Api; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@RestController | ||
@Api(value = "Home Resource") | ||
public class HomeController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); | ||
|
||
@PostMapping("/home") | ||
public String getAuthorizationCode(@RequestParam("code") String authorizationCode) throws GenericErrorException { | ||
return authorizationCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters