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. I need to read the profiles of a user in linked in. Can anyone suggest me any links or examples to do the same in Java.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You need to utilize some OAuth library. Try looking into Scribe.
Here's one of the LinkedIn examples in Java:
package org.scribe.examples;
import java.util.Scanner;
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
public class LinkedInExample
{
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~/connections:(id,last-name)";
public static void main(String[] args)
{
OAuthService service = new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey("CiEgwWDkA5BFpNrc0RfGyVuSlOh4tig5kOTZ9q97qcXNrFl7zqk-Ts7DqRGaKDCV")
.apiSecret("dhho4dfoCmiQXrkw4yslork5XWLFnPSuMR-8gscPVjY4jqFFHPYWJKgpFl4uLTM6")
.build();
Scanner in = new Scanner(System.in);
System.out.println("=== LinkedIn's OAuth Workflow ===");
System.out.println();
// Obtain the Request Token
System.out.println("Fetching the Request Token...");
Token requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();
System.out.println("Now go and authorize Scribe here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());
System.out.println();
// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
Token accessToken = service.getAccessToken(requestToken, verifier);
System.out.println("Got the Access Token!");
System.out.println("(if your curious it looks like this: " + accessToken + " )");
System.out.println();
// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getBody());
System.out.println();
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
}
}
The above can be found in this part of the Scribe repository.
回答2:
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<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> 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";
}
}