我正在编写一个连接到XMPP服务器的聊天应用程序,如果用户选择,我想让他们选择连接到他们的谷歌聊天帐户,而无需输入凭据… 我使用google的 javascript api弹出google登录表单,成功登录访问
我使用google的 javascript api弹出google登录表单,成功登录访问令牌后会生成.现在使用该访问令牌和用户电子邮件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()得到一个异常“用户名或密码不正确”
要做到这一点,我将获得使用谷歌帐户的许可,获取令牌并使用令牌验证谷歌谈话(XMPP服务器,使用Smack).
问题是..我该怎么做?我的意思是,如果我知道登录和令牌,我如何对GTalk服务器进行身份验证?
任何帮助将非常感谢……