我正在编写一个连接到 XMPP 服务器的聊天应用程序,如果用户选择,我想让他们选择连接到他们的 google 聊天帐户,而无需输入凭据......我使用了 google 的 javascript api弹出谷歌登录表单,成功登录后将生成访问令牌。现在使用该访问令牌和用户电子邮件 ID,我想与 xmpp 服务器通信,以便用户可以与他们的 gtalk 朋友聊天。
我搜索了很多,但没有找到解决方案。无论我找到所需的用户密码,但我想使用访问令牌。
SASLAuthentication.registerSASLMechanism("X-OAUTH2", GoogleConnectSASLMechanism.class);
SASLAuthentication.supportSASLMechanism("X-OAUTH2", 0);
config = new ConnectionConfiguration(server, 5222, 'gmail.com');
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(SecurityMode.enabled);
config.setReconnectionAllowed(true);
connection = new XMPPConnection(config);
connection.connect();
connection.login(username, session_key, "Chat");
setServer(SERVER_TYPE.GTALK);
GoogleConnectSASLMechanism.java 代码如下:-
package org.jivesoftware.smack;
import java.io.IOException;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.callback.CallbackHandler;
import javax.security.sasl.Sasl;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.Base64;
public class GoogleConnectSASLMechanism extends SASLMechanism {
public static final String NAME="X-OAUTH2";
public GoogleConnectSASLMechanism(SASLAuthentication saslAuthentication) {
super(saslAuthentication);
}
@Override
protected String getName() {
return NAME;
}
static void enable() { }
@Override
protected void authenticate() throws IOException, XMPPException
{
String authCode = password;
String jidAndToken = "\0" + URLEncoder.encode( authenticationId, "utf-8" ) + "\0" + authCode;
StringBuilder stanza = new StringBuilder();
//stanza.append( "<auth mechanism=\"" ).append( getName() );
//stanza.append( "\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" );
// stanza.append( new String(Base64.encode( jidAndToken.getBytes( "UTF-8" ), Base64.DEFAULT ) ) );
stanza.append( "<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" mechanism=\"" ).append( getName() );
stanza.append( "\" auth:service=\"oauth2\"" );
stanza.append( "\" xmlns:auth=\"http://www.google.com/talk/protocol/auth\">" );
stanza.append( "\" base64(\"\\0"+user_name+"\\0" + authCode+")" );
stanza.append( "</auth>" );
//Log.v("BlueTalk", "Authentication text is "+stanza);
// Send the authentication to the server
getSASLAuthentication().send( new Auth2Mechanism(stanza.toString()) );
}
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();
}
}
}
但我从 connection.login() 得到一个异常,即“用户名或密码不正确”
为此,我将获得使用 google 帐户的权限,获取令牌并使用令牌对 google talk(XMPP 服务器,使用 Smack)进行身份验证。
问题是..我该怎么做?我的意思是,如果我知道登录名和令牌,如何向 GTalk 服务器进行身份验证?
任何帮助将不胜感激... :)