LinkedIn API Authentication using Java

后端 未结 2 507
情话喂你
情话喂你 2021-02-08 23:39

I am new to Linked In API for authentication. I went the API document provided by LinkedIn. It has samples for RUBY,PYTHON and PHP. But i am asked to achieve the same using Java

2条回答
  •  庸人自扰
    2021-02-09 00:08

    I hope this will work for you... i use spring framework

    package yraunaj.controller;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.client.RestTemplate;
    
    @Controller
    public class HomeController {
    
        //get client id and client Secret by creating a app in 
        //https://www.linkedin.com developers
        //and set your redirect url
        public String clientId="**********";
        public String clientSecret="***********";
        public String redirectUrl="http://localhost:8080/yraunaj/home"; 
    
        //create button on your page and hit this get request
        @GetMapping("/authorization")
        public String authorization() {
            String authorizationUri="https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id="+clientId+"&redirect_uri="+redirectUrl+"&state=asasasasasas&scope=r_basicprofile%20r_emailaddress";
            return "redirect:" + authorizationUri;
        }
    
        //after login in your linkedin account your app will hit this get request
        @GetMapping("/home")
    
        //now store your authorization code
        public String home(@RequestParam("code") String authorizationCode) throws JSONException {
    
            //to trade your 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+"";
    
            // linkedin api to get linkedidn profile detail
            String linedkinDetailUri = "https://api.linkedin.com/v1/people/~:(id,first-name,email-address,last-name,headline,picture-url,industry,summary)?format=json";
    
            //store your access token
            RestTemplate restTemplate = new RestTemplate();
            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 entity = new HttpEntity("parameters", headers);
            ResponseEntity linkedinDetailRequest = restTemplate.exchange(linedkinDetailUri, HttpMethod.GET, entity, String.class);
            //store json data
            JSONObject jsonObjOfLinkedinDetail = new JSONObject(linkedinDetailRequest.getBody());
            //print json data
            System.out.println(jsonObjOfLinkedinDetail);
    
            return "home";
        }
    }
    

提交回复
热议问题