我在想我是否可以使用Java爲LinkedIn開發桌面應用程序。我知道它可以輕鬆地作爲Web應用程序來完成,但是完全是桌面應用程序,這有可能嗎? 我看了一下linkedin api和Java Wrapper for LinkedIn。代碼是針對Web應用程序解釋的。我如何在Java桌面應用程序中管理它,特別是授權部分? oAuth使用Swing?我可以使用Java爲LinkedIn開發桌面應用程序嗎?
請指導我以正確的方式。
我在想我是否可以使用Java爲LinkedIn開發桌面應用程序。我知道它可以輕鬆地作爲Web應用程序來完成,但是完全是桌面應用程序,這有可能嗎? 我看了一下linkedin api和Java Wrapper for LinkedIn。代碼是針對Web應用程序解釋的。我如何在Java桌面應用程序中管理它,特別是授權部分? oAuth使用Swing?我可以使用Java爲LinkedIn開發桌面應用程序嗎?
請指導我以正確的方式。
經過了很長時間的測試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 不要忘了包括抄寫罐子和Apache公地編解碼器(用於Base64
)
是的,你可以玩API並利用LinkedIn API中的Web服務。
但是,整個過程必須通過使用HTTP請求等並解析響應以在JForm上呈現它來實現。
編輯:啊!你是完全獨立的:-)由於XML ..
謝謝安德魯。我只想知道是否可以讀取/寫入請求和回覆。現在我知道他們是,我可以嘗試。 :) – 2011-06-06 06:25:00
如果你不能弄清楚如何將用戶重定向到Web瀏覽器,並讓瀏覽器重定向回到你的應用程序,檢查出「越界」 (又名「oob」)選項用於OAuth回調。這會在授權您的應用程序後向成員顯示代碼,這些代碼可以鍵入到您的Java應用程序中。
非常感謝,亞當。 oob正是我一直在尋找的:) – 2011-06-13 14:50:30
太棒了!很高興爲你工作。 – 2011-06-14 07:19:12
如果你打算使用擺動,最好的學習如何拼寫(一個',而不是兩個)。 – 2011-06-04 18:45:18
謝謝安德魯... :) – 2011-06-06 06:23:34