简体   繁体   English

我可以使用 Java 为 LinkedIn 开发桌面应用程序吗?

[英]Can I develop a Desktop App for LinkedIn using Java?

I was wondering if I can develop a Desktop App for LinkedIn using Java.我想知道是否可以使用 Java 为 LinkedIn 开发桌面应用程序。 I know it can be done as a web application easily, but a completely desktop application, is it possible?我知道它可以作为 web 应用程序轻松完成,但完全是桌面应用程序,这可能吗? I had a look at the linkedin api's and Java Wrapper for LinkedIn .我查看了 LinkedIn 的 LinkedIn api 和 Java Wrapper The code was explained for a web application.该代码针对 web 应用程序进行了解释。 How do I manage that in a java desktop app, specifically the authorization part?如何在 java 桌面应用程序中管理它,特别是授权部分? oAuth using Swing? oAuth 使用 Swing?

Please direct me in the right way.请以正确的方式指导我。

Yes you can it's all about playing with the API and utilizing the web services packed within the LinkedIn's API.是的,您可以使用 API 并利用 LinkedIn 的 API 中包含的 web 服务。

However, the entire process has to be implemented by using the HTTP requests etc and by parsing the response to render it on the JForm.但是,整个过程必须通过使用 HTTP 请求等并通过解析响应以将其呈现在 JForm 上来实现。

EDIT: Ahh: you are totally independent.-) thanks to XML..编辑:啊:你是完全独立的。-)感谢 XML..

After a very long time of testing with oAuth (with my own wrappers), I settled for Scribe which is a Java Wrapper for almost all oAuth mechanisms.在使用 oAuth(使用我自己的包装器)进行了很长时间的测试后,我选择了 Scribe,它是几乎所有 oAuth 机制的 Java 包装器。 To include Linkedin in a Desktop client, as Adam Trachtenberg (Thank you again) suggested, oob option was used, ie, after logging in, a code generated by linkedin has to be entered in our Client so that it can be validated against the requested url.要将 Linkedin 包含在桌面客户端中,正如 Adam Trachtenberg(再次感谢您)所建议的那样,使用了 oob 选项,即登录后,必须在我们的客户端中输入由 linkedin 生成的代码,以便可以根据请求对其进行验证url。 Hope this is useful for someone.希望这对某人有用。

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) throws IOException
  {
    OAuthService service = new ServiceBuilder()
                                .provider(LinkedInApi.class)
                                .apiKey("YourApiKey")
                                .apiSecret("YourApiSecret")
                                .build();
    Scanner in = new Scanner(System.in);
    //BareBonesBrowserLaunch.openURL("www.google.com");
    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:");
    String authURL = service.getAuthorizationUrl(requestToken);
    System.out.println(authURL);
    BareBonesBrowserLaunch.openURL("www.google.com");
    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 BareBonesBrowserLaunch is used to launch the default browser with the Linkedin URL for the token request in most OS's. BareBonesBrowserLaunch用于在大多数操作系统中启动带有 Linkedin URL 的默认浏览器,用于令牌请求。 Since the Desktop part is not available in Java 1.5, the BareBonesBrowserLaunch solves the problem.由于Desktop部分在 Java 1.5 中不可用, BareBonesBrowserLaunch解决了这个问题。

public class BareBonesBrowserLaunch {

   static final String[] browsers = { "google-chrome", "firefox", "opera",
      "epiphany", "konqueror", "conkeror", "midori", "kazehakase", "mozilla" };
   static final String errMsg = "Error attempting to launch web browser";

   public static void openURL(String url) {
      try {  //attempt to use Desktop library from JDK 1.6+
         Class<?> d = Class.forName("java.awt.Desktop");
         d.getDeclaredMethod("browse", new Class[] {java.net.URI.class}).invoke(
            d.getDeclaredMethod("getDesktop").invoke(null),
            new Object[] {java.net.URI.create(url)});
         //above code mimicks:  java.awt.Desktop.getDesktop().browse()
         }
      catch (Exception ignore) {  //library not available or failed
         String osName = System.getProperty("os.name");
         try {
            if (osName.startsWith("Mac OS")) {
               Class.forName("com.apple.eio.FileManager").getDeclaredMethod(
                  "openURL", new Class[] {String.class}).invoke(null,
                  new Object[] {url});
               }
            else if (osName.startsWith("Windows"))
               Runtime.getRuntime().exec(
                  "rundll32 url.dll,FileProtocolHandler " + url);
            else { //assume Unix or Linux
               String browser = null;
               for (String b : browsers)
                  if (browser == null && Runtime.getRuntime().exec(new String[]
                        {"which", b}).getInputStream().read() != -1)
                     Runtime.getRuntime().exec(new String[] {browser = b, url});
               if (browser == null)
                  throw new Exception(Arrays.toString(browsers));
               }
            }
         catch (Exception e) {
            JOptionPane.showMessageDialog(null, errMsg + "\n" + e.toString());
            }
         }
      }

   }

The LinkedInExample is taken mostly from this library - https://github.com/fernandezpablo85/scribe-java/downloads Don't forget to include the Scribe jar and apache commons-codec (for Base64 ) The LinkedInExample is taken mostly from this library - https://github.com/fernandezpablo85/scribe-java/downloads Don't forget to include the Scribe jar and apache commons-codec (for Base64 )

If you can't figure out how to redirect the user to a web browser and have the browser redirect back to your application, check out the "out of bounds" (aka "oob") option for the OAuth callback.如果您不知道如何将用户重定向到 web 浏览器并将浏览器重定向回您的应用程序,请查看 OAuth 回调的“越界”(又名“oob”)选项。 This will display a code to the member after they authorize your application, which they can type into your Java app.这将在会员授权您的应用程序后向会员显示一个代码,他们可以将其输入到您的 Java 应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 java 开发 iPhone 应用程序吗? - Can I develop an iPhone app using java? 开发用于学习目的的Java桌面应用程序? - Develop java desktop app for learning purpose? 我可以将 Firebase 与 Java 桌面应用一起使用吗? - Can I use Firebase with java desktop app? 我可以使用客户端登录中的Firebase ID令牌来使用Java SDK验证Java桌面应用程序吗? - Can I use the Firebase ID token from a client login to authenticate a Java desktop app using the Java SDK? 我可以使用java HtmlUnit库从linkedIn中提取信息吗? - Can I extract information from linkedIn using java HtmlUnit library? 我可以在Eclipse中使用Android SDK Java开发平板电脑吗? - Can i develop for tablet using the Android SDK Java in Eclipse? 如何隐藏使用Java桌面应用程序构建并由Android应用程序使用的文件的内容? - How can i conceal the content of a file that is constructed using a Java desktop application and used by an Android app? 使用Linkedin-j的Linkedin桌面应用程序? - Linkedin Desktop application using Linkedin-j? 我可以使用Java为Elementary OS AppCenter开发吗? - Can I develop for Elementary OS AppCenter in Java? 如何使用Java开发“ xdcc发送”? - How I can develop an “xdcc send” in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM