gpt4 book ai didi

jsp - 使用 authToken 连接到 GTalk 服务器(XMPP、Smack)

转载 作者:行者123 更新时间:2023-12-05 00:31:43 31 4
gpt4 key购买 nike

我正在编写一个连接到 XMPP 服务器的聊天应用程序,如果用户选择,我想让他们选择连接到他们的谷歌聊天帐户,而无需输入凭据......
我使用谷歌的 javascript api 弹出谷歌登录表单,成功登录后将生成访问 token 。现在使用该访问 token 和用户电子邮件 ID,我想与 xmpp 服务器进行通信,以便用户可以与他们的 gtalk friend 聊天。

我搜索了很多,但没有找到解决方案。无论我找到了所需的用户密码,但我想使用访问 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 代码如下:-
    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 帐户的权限,获取 token 并使用 token 对 google talk(XMPP 服务器,使用 Smack)进行身份验证。

问题是……我该怎么做?我的意思是,如果我知道登录名和 token ,我该如何向 GTalk 服务器进行身份验证?

任何帮助将不胜感激...:)

最佳答案

维杰,

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

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

它与您原来的身份验证功能相同。我刚刚更改了节。

关于jsp - 使用 authToken 连接到 GTalk 服务器(XMPP、Smack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14320222/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com