我在我的聊天應用程序中使用GoogleTalk XMPP。無法通過使用用戶名和AuthToken與Google authentication
創建XMPP連接。使用機制X-OAUTH2的Gtalk XMPP SASL認證失敗?
現在我使用GoogleAuth2
代替authentication
。我嘗試使用access_token和電子郵件進行身份驗證。通過使用SASLMechanism
。但我不能夠連接到XMPP SERV 呃,它給錯誤這樣SASL authentication failed using mechanism X-OAUTH2
ConnectionConfiguration config = new ConnectionConfiguration(server_host, SERVER_PORT, SERVICE_NAME);
config.setSASLAuthenticationEnabled(true);
m_connection = new XMPPConnection(config);
SASLAuthentication.registerSASLMechanism("X-OAUTH2", GoogleConnectSASLMechanism.class);
SASLAuthentication.supportSASLMechanism("X-OAUTH2", 0);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
try {
m_connection.connect();
} catch (XMPPException e) {
e.printStackTrace();
}
這是類我使用SASLMechanism.
public class GoogleConnectSASLMechanism extends SASLMechanism {
public static final String NAME = "X-OAUTH2";
private String username = "";
private String sessionKey = "";
public GoogleConnectSASLMechanism(SASLAuthentication saslAuthentication) {
super(saslAuthentication);
}
@Override
protected String getName() {
return NAME;
}
static void enable() {
}
@Override
protected void authenticate() throws IOException, XMPPException {
final StringBuilder stanza = new StringBuilder();
byte response[] = null;
stanza.append("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"" +
"mechanism=\"X-OAUTH2\"" +
"auth:service=\"oauth2\"" +
"xmlns:auth= \"http://www.google.com/talk/protocol/auth\">");
String composedResponse = "\0" + username + "\0" + sessionKey;
response = composedResponse.getBytes("UTF-8");
String authenticationText = "";
if (response != null) {
authenticationText = Base64.encodeBytes(response, Base64.DONT_BREAK_LINES);
}
stanza.append(authenticationText);
stanza.append("</auth>");
// Send the authentication to the server
Packet p=new Packet() {
@Override
public String toXML() {
return stanza.toString();
}
};
getSASLAuthentication().send(p);
}
public class Auth2Mechanism extends Packet {
String stanza;
public Auth2Mechanism(String txt) {
stanza = txt;
}
public String toXML() {
return stanza;
}
}
/**
* Initiating SASL authentication by select a mechanism.
*/
public class AuthMechanism extends Packet {
final private String name;
final private String authenticationText;
public AuthMechanism(String name, String authenticationText) {
if (name == null) {
throw new NullPointerException(
"SASL mechanism name shouldn't be null.");
}
this.name = name;
this.authenticationText = authenticationText;
}
public String toXML() {
StringBuilder stanza = new StringBuilder();
stanza.append("<auth mechanism=\"").append(name);
stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
if (authenticationText != null
&& authenticationText.trim().length() > 0) {
stanza.append(authenticationText);
}
stanza.append("</auth>");
return stanza.toString();
}
}
}
如何使用SASL的Google Auth
認證機制?
您發佈在頂部的網址不工作..請發佈相關的工作網址。 – 2013-04-08 06:18:54
謝謝你這就是爲什麼我在某些時候得到這個錯誤做工精細...... – 2013-04-08 07:18:41
「使用機制SASL認證失敗普通紙:」 – 2013-04-08 11:12:16