2011-03-31 18 views
4

我試圖用拍擊API.But來整合Facebook聊天Facebook的聊天失敗,我得到一個錯誤,告訴認證使用MD5摘要失敗...SASL認證,同時整合利用拍擊

下面就爲驗證碼:

SASLAuthentication.registerSASLMechanism("DIGEST-MD5", SASLDigestMD5Mechanism.class); 
    SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0); 

    ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222); 
    connection = new XMPPConnection(config); 
    config.setSASLAuthenticationEnabled(true); 
    connection.connect(); 
    connection.login(userName, password); 
下面

是錯誤我得到文我運行它:

Exception in thread "main" SASL authentication failed using mechanism DIGEST-MD5: 
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:325) 
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395) 
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349) 
    at JabberSmackAPIFacebook.login(JabberSmackAPIFacebook.java:31) 
    at JabberSmackAPIFacebook.main(JabberSmackAPIFacebook.java:77) 

我可以成功連接到每增加一個,但我沒有成功維生素FB ... 可以sumone電話給我wat問題

+0

嘿,任何人都可以建議一個解決方案plssss..runnin超時 – enthusiastic 2011-04-03 18:37:08

回答

0

有一個巨大的線程在Ignite處理這個問題。你可能想看看它,因爲有幾種Java和Android的解決方案似乎可行。

+0

是啊,我試過所給出的解決方案,但沒有一個人工作...哈維在論壇上發佈的問題,但沒有答覆那裏 – enthusiastic 2011-04-06 04:28:40

0

我已成功使用DIGEST-MD5連接到Facebook,您發佈的代碼看起來不錯。 但是我們仍然需要檢查你的SASLDigestMD5Mechanism類的內容

我用在這裏提供了成功的類

http://community.igniterealtime.org/message/200878#200878

你也需要注意到,在你有DIGEST-MD5機制用您的facebook用戶名登錄,而不是通過電子郵件地址登錄。默認情況下,Facebook的帳戶沒有用戶名,你必須創建一個最前一頁,您可以檢查在這裏:

http://www.facebook.com/username/

1

對我來說,解決辦法是不包含用戶名中的主機部分在沒有DNS SRV的情況下撥打login(),而不是Google Talk服務。這也是described in the ignite forums.

例如,

connection.login("[email protected]", "password", "resource"); 

變得

connection.login("user", "password", "resource"); 
0

MainActivity.java

public class MainActivity extends Activity { 

XMPPConnection xmpp; 
ArrayList<HashMap<String, String>> friends_list; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Session.openActiveSession(this, true, new StatusCallback() { 

     @Override 
     public void call(Session session, SessionState state, Exception exception) { 

      if (session.isOpened()){ 
       new testLoginTask().execute(); 
      } 
     } 
    }); 

} 

private class testLoginTask extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 

     testLogin(); 

     return null; 
    } 

} 

private void testLogin(){ 

    ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
    config.setSASLAuthenticationEnabled(true); 
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
    config.setDebuggerEnabled(true); 


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
     config.setTruststoreType("AndroidCAStore"); 
     config.setTruststorePassword(null); 
     config.setTruststorePath(null); 
    } else { 
     config.setTruststoreType("BKS"); 
     String path = System.getProperty("javax.net.ssl.trustStore"); 
     if (path == null) 
      path = System.getProperty("java.home") + File.separator + "etc" 
              + File.separator + "security" + File.separator 
              + "cacerts.bks"; 
     config.setTruststorePath(path); 
    } 


    xmpp = new XMPPConnection(config); 
    SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
    SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 

    try { 
     xmpp.connect(); 
     Log.i("XMPPClient","Connected to " + xmpp.getHost()); 

    } catch (XMPPException e1) { 
     Log.i("XMPPClient","Unable to " + xmpp.getHost()); 

     e1.printStackTrace(); 
    } 
    try { 


     String apiKey = Session.getActiveSession().getApplicationId(); 
     String sessionKey = Session.getActiveSession().getAccessToken(); 
     String sessionSecret = "replace with your app secret key"; 

     xmpp.login(apiKey + "|" + sessionKey, sessionSecret , "Application"); 

     Log.i("XMPPClient"," its logined "); 

     Log.i("Connected",""+xmpp.isConnected()); 

     if (xmpp.isConnected()){ 
      Presence presence = new Presence(Presence.Type.available); 
      xmpp.sendPacket(presence); 

     } 

    } catch (XMPPException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
} 

SASLXFacebookPlatformMechanism.java

public class SASLXFacebookPlatformMechanism extends SASLMechanism{ 

private static final String NAME    = "X-FACEBOOK-PLATFORM"; 
private String    apiKey   = ""; 
private String    accessToken  = ""; 

/** 
* Constructor. 
*/ 
public SASLXFacebookPlatformMechanism(SASLAuthentication saslAuthentication) { 
    super(saslAuthentication); 
} 

@Override 
protected void authenticate() throws IOException, XMPPException { 
    getSASLAuthentication().send(new AuthMechanism(NAME, "")); 
} 

@Override 
public void authenticate(String apiKey, String host, String accessToken) throws IOException, XMPPException { 
    if (apiKey == null || accessToken == null) { 
     throw new IllegalArgumentException("Invalid parameters"); 
    } 

    this.apiKey = apiKey; 
    this.accessToken = accessToken; 
    this.hostname = host; 

    String[] mechanisms = { "DIGEST-MD5" }; 
    Map<String, String> props = new HashMap<String, String>(); 
    this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this); 
    authenticate(); 
} 

@Override 
public void authenticate(String username, String host, CallbackHandler cbh) throws IOException, XMPPException { 
    String[] mechanisms = { "DIGEST-MD5" }; 
    Map<String, String> props = new HashMap<String, String>(); 
    this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh); 
    authenticate(); 
} 

@Override 
protected String getName() { 
    return NAME; 
} 

@Override 
public void challengeReceived(String challenge) throws IOException { 
    byte[] response = null; 

    if (challenge != null) { 
     String decodedChallenge = new String(Base64.decode(challenge)); 
     Map<String, String> parameters = getQueryMap(decodedChallenge); 

     String version = "1.0"; 
     String nonce = parameters.get("nonce"); 
     String method = parameters.get("method"); 

     long callId = new GregorianCalendar().getTimeInMillis(); 

     String composedResponse = "api_key=" 
       + URLEncoder.encode(apiKey, "utf-8") + "&call_id=" + callId 
       + "&method=" + URLEncoder.encode(method, "utf-8") 
       + "&nonce=" + URLEncoder.encode(nonce, "utf-8") 
       + "&access_token=" 
       + URLEncoder.encode(accessToken, "utf-8") + "&v=" 
       + URLEncoder.encode(version, "utf-8"); 

     response = composedResponse.getBytes("utf-8"); 
    } 

    String authenticationText = ""; 

    if (response != null) { 
     authenticationText = Base64.encodeBytes(response, 
       Base64.DONT_BREAK_LINES); 
    } 

    // Send the authentication to the server 
    getSASLAuthentication().send(new Response(authenticationText)); 
} 

private Map<String, String> getQueryMap(String query) { 
    Map<String, String> map = new HashMap<String, String>(); 
    String[] params = query.split("\\&"); 

    for (String param : params) { 
     String[] fields = param.split("=", 2); 
     map.put(fields[0], (fields.length > 1 ? fields[1] : null)); 
    } 

    return map; 
} 

}