【问题标题】:LinkedIn API Authentication using Java使用 Java 的 LinkedIn API 身份验证
【发布时间】:2021-09-17 00:35:47
【问题描述】:

我是用于身份验证的 Linked In API 的新手。我去了LinkedIn提供的API文档。 它有 RUBY、PYTHON 和 PHP 的示例。但我被要求使用 Java 实现相同的目标。我需要阅读链接中用户的个人资料。谁能建议我任何链接或示例以在 Java 中做同样的事情。

【问题讨论】:

    标签: java rest linkedin


    【解决方案1】:

    您需要使用一些 OAuth 库。尝试查看Scribe

    这是一个用 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! :)");
      }
    
    }
    

    以上内容可在 Scribe 存储库的 this 部分中找到。

    【讨论】:

      【解决方案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";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 2017-10-13
        • 1970-01-01
        • 2021-09-21
        • 2023-03-19
        相关资源
        最近更新 更多