使用authToken连接到GTalk服务器(XMPP,Smack)(Connect to GTalk server (XMPP, Smack) using an authToken)

编程入门 行业动态 更新时间:2024-10-27 04:28:59
使用authToken连接到GTalk服务器(XMPP,Smack)(Connect to GTalk server (XMPP, Smack) using an authToken)

我正在写一个连接到XMPP服务器的聊天应用程序,如果用户选择,我想给他们连接到他们的谷歌聊天帐户的选项,而不必输入凭据...我使用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()得到一个异常“用户名或密码不正确”

要做到这一点,我将获得使用谷歌帐户的许可,获取令牌并使用令牌验证谷歌谈话(XMPP服务器,使用Smack)。

问题是..我该怎么办? 我的意思是,如果我知道登录和令牌,我如何向GTalk服务器进行身份验证?

任何帮助将非常感谢...... :)

I'm writing a chat app which is connecting to a XMPP server, and if the user chooses, I want to give them the option to connect to their google chat account, without having to enter the credentials... I used google's javascript api to popout the google login form and after successful login access token will generate. Now using that access token and users email id i want to communicate with xmpp server so that users can chat with their gtalk friends.

I searched a lot but didn't found the solution. Whatever i found required users password but i want to use access token.

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 code is as follows:-

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(); } } }

But I got an Exception from connection.login() that "username or password are not correct"

To do this, I'd get the permission to use the google account, get the token and authenticate to google talk (XMPP server, using Smack) using the token..

The problem is.. how do I do that? I mean, how do I authenticate to the GTalk server if I know the login and the token?

Any help would be highly appreciated...:)

最满意答案

维杰,

更改您的身份验证功能,如下所示:

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); }

与原始身份验证功能相同。 我刚刚改变了节。

Vijay,

Change your authenticate function as follows :

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); }

Its same as your original authenticate function. I have just changed stanza.

更多推荐

本文发布于:2023-07-28 01:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   服务器   GTalk   authToken   server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!