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

发布于 2024-11-13 09:44:55 字数 280 浏览 0 评论 0原文

我想知道是否可以使用 Java 为 LinkedIn 开发桌面应用程序。我知道它可以很容易地作为一个网络应用程序来完成,但是一个完全的桌面应用程序,可能吗? 我查看了 linkedin api 和 LinkedIn 的 Java 包装器。 该代码是针对 Web 应用程序进行解释的。我如何在java桌面应用程序中管理它,特别是授权部分? 使用 Swing 进行 oAuth?

请以正确的方式指导我。

I was wondering if I can develop a Desktop App for LinkedIn using Java. I know it can be done as a web application easily, but a completely desktop application, is it possible?
I had a look at the linkedin api's and Java Wrapper for LinkedIn.
The code was explained for a web application. How do I manage that in a java desktop app, specifically the authorization part?
oAuth using Swing?

Please direct me in the right way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-11-20 09:44:55

经过很长时间的 oAuth(使用我自己的包装器)测试后,我选择了 Scribe,它是几乎所有 oAuth 机制的 Java 包装器。要将 Linkedin 包含在桌面客户端中,正如 Adam Trachtenberg(再次感谢您)建议的那样,使用了 oob 选项,即登录后,必须在我们的客户端中输入 linkedin 生成的代码,以便可以根据请求进行验证网址。希望这对某人有用。

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

}

BareBonesBrowserLaunch 用于在大多数操作系统中通过 Linkedin URL 启动默认浏览器以进行令牌请求。由于 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());
            }
         }
      }

   }

LinkedInExample 大部分取自此库 - https://github.com /fernandezpablo85/scribe-java/downloads
不要忘记包含 Scribe jar 和 apache commons-codec(用于 Base64

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. To include Linkedin in a Desktop client, as Adam Trachtenberg (Thank you again) suggested, oob option was used, i.e., 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. 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. Since the Desktop part is not available in Java 1.5, the BareBonesBrowserLaunch solves the problem.

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)

笨死的猪 2024-11-20 09:44:55

是的,您可以,这一切都是为了使用 API 并利用 LinkedIn API 中包含的 Web 服务。

然而,整个过程必须通过使用 HTTP 请求等并解析响应以将其呈现在 JForm 上来实现。

编辑:啊!你是完全独立的:-) 感谢 XML..

Yes you can it's all about playing with the API and utilizing the web services packed within the LinkedIn's API.

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.

EDIT: Ahh! you are totally independent :-) thanks to XML..

伪装你 2024-11-20 09:44:55

如果您不知道如何将用户重定向到 Web 浏览器并将浏览器重定向回您的应用程序,请查看 OAuth 回调的“越界”(又名“oob”)选项。这将在成员授权您的应用程序后向其显示一个代码,他们可以将其输入到您的 Java 应用程序中。

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. This will display a code to the member after they authorize your application, which they can type into your Java app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文