我想知道我是否可以使用Java为LinkedIn开发桌面应用程序。我知道它可以作为Web应用程序轻松完成,但是完全桌面应用程序,是否可能? 我看了一下linkedin api和Java Wrapper for LinkedIn。 代码是针对Web应用程序解释的。如何在Java桌面应用程序中管理它,特别是授权部分? oAuth使用Swing?
请以正确的方式指导我。
答案 0 :(得分:2)
是的,您可以使用API并利用LinkedIn API中包含的网络服务。
但是,整个过程必须通过使用HTTP请求等来实现,并通过解析响应来在JForm上呈现它。
编辑:啊!你是完全独立的:-)感谢XML ..答案 1 :(得分:2)
在使用oAuth(使用我自己的包装器)进行了很长时间的测试之后,我选择了Scribe,这是几乎所有oAuth机制的Java Wrapper。要将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启动令牌请求的默认浏览器。由于{1.5}中没有Desktop
部分,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
)
答案 2 :(得分:0)
如果您无法弄清楚如何将用户重定向到Web浏览器并将浏览器重定向回应用程序,请查看OAuth回调的“越界”(又名“oob”)选项。这将在成员授权您的应用程序后显示该成员的代码,然后他们可以在您的Java应用程序中输入该代码。