2013-10-29 114 views
3

我需要使用Java登錄來獲取我的user_accessToken。我有使用FacebookOAuthResult嗎?如果是,如何? 我寫了一個Applet登錄到Facebook。它有效,但我無法獲得令牌。用Java登錄Facebook獲取用戶accessToken

... 
String GraphURL = "https://graph.facebook.com/me/friends?&access_token=" + token; 
URL newURL = URL(GraphURL); 
HttpsURLConnection https = (HttpsURLConnection)newURL.openConnection(); 
https.setRequestMethod("HEAD"); 
https.setUseCache(false); 
... 

//open a connection window like: 
if(Desktop.isDesktopSupported()) 
{ 
Desktop.getDesktop().browse(new URI(GraphURL)); 
} 
else if(... 

這裏獲取令牌。這是正確的?

String getTokenUrl = "https://www.facebook.com/dialog/oauth?client_id=MY_APP_ID&redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&response_type=token&display=popup&scope=user_about_me%2Cread_stream%2C%20share_item"; 

Desktop desktop = Desktop.getDesktop(); 
desktop.browse(new URL(getTokenUrl).toURI()); 

URL tokenURL = new URL(getTokenUrl); 
URLConnection connect = tokenURL.openConnection(); 

BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream())); 

String inputLine; 
StringBuffer bufferr = new StringBuffer(); 
while ((inputLine = in.readLine()) != null) 
bufferr.append(inputLine + "\n"); 
in.close(); 

token = bufferr.toString(); 

HttpURLConnection myWebClient = (HttpURLConnection)url.openConnection(); 
myWebClient.setRequestMethod("HEAD"); 
myWebClient.setUseCaches(false); 
//webClient.. 
try 
{ 
String gAntwort = myWebClient.getResponseMessage(); 

if (myWebClient.getResponseCode() != HttpURLConnection.HTTP_OK) 
{ 
//Not OK 
//JOptionPane.showMessageDialog(null, conn.getResponseMessage(), "URL Response", JOptionPane.INFORMATION_MESSAGE); 
} 
init(); 
} catch (Exception d){ 

} 

回答

4

我不認爲幫助有一種方式來獲得用戶的訪問令牌編程。 Facebook要求用戶同意,以前登錄的用戶需要按下「好」按鈕才能獲得新的access_token

但是,有一種方法可以獲得application access token,如果這是您的意圖。

public static void main(String[] args) { 
    try { 

     String myAppId = "XXX"; 
     String myAppSecret = "XXX"; 
     String redirectURI = "http://localhost:8080/"; 

     String uri = "https://graph.facebook.com/oauth/access_token?client_id=" + 
       myAppId + 
       "&client_secret=" + myAppSecret + 
       "&grant_type=client_credentials" + 
       "&redirect_uri=" + redirectURI + 
       "&scope=user_about_me"; 

     URL newURL = new URL(uri); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     InputStream is = newURL.openStream(); 
     int r; 
     while ((r = is.read()) != -1) { 
      baos.write(r); 
     } 
     String response = new String(baos.toByteArray()); 
     is.close(); 
     baos.close(); 

     String TOKEN_INDEX = "accesss_token="; 
     String token = response.substring(TOKEN_INDEX.length()-1); 

     //API Call using the application access token 
     String graph = "https://graph.facebook.com/v2.2/" + myAppId + 
       "?access_token=" + token; 

     URL graphURL = new URL(graph); 
     HttpURLConnection myWebClient = (HttpURLConnection) graphURL.openConnection(); 

     String responseMessage = myWebClient.getResponseMessage(); 

     if (myWebClient.getResponseCode() != HttpURLConnection.HTTP_OK) { 
      System.out.println(myWebClient.getResponseCode() + " " + responseMessage); 
     } else { 
      System.out.println(responseMessage); 
     } 
     myWebClient.disconnect(); 

    } catch (Exception e) { 
     System.err.println(e); 
    } 
} 

注意,因爲這要求使用你的應用程序的祕密,它決不能在客戶端代碼或可能被反編譯的應用二進制文件中提出。不要與任何人共享您的應用祕密,這一點很重要。因此,只能使用服務器端代碼進行此API調用。

這裏閱讀:https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

順便說一句,我們在Stormpath支持真正simple social login整合(谷歌,Github上,Facebook和LinkedIn)

相關問題