使用Java API LinkedIn认证(LinkedIn API Authentication

2019-08-02 08:00发布

我是新来的链接在API进行身份验证。 我去由LinkedIn提供的API文档。 它有红宝石,Python和PHP样本。 但我要求实现使用Java相同。 我需要阅读在链接的用户的配置文件。任何人都可以给我建议的任何链接或例子做同样在Java中。

Answer 1:

你需要利用一些OAuth的库。 试着看抄写

下面是在Java中,LinkedIn的例子之一:

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! :)");
  }

}

上述可以发现这个抄写员库的一部分。



Answer 2:

我希望这会为你工作...我使用Spring框架

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";
    }
}


文章来源: LinkedIn API Authentication using Java