gpt4 book ai didi

android - 如何使用 Smack API 使用 AccountManager 的身份验证 token 对 Google Talk 进行身份验证?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:43 25 4
gpt4 key购买 nike

这个问题类似于:Authenticate to Google Talk (XMPP, Smack) using an authToken

  1. 我有 android.accounts.AccountManager 类及其获取 Google 帐户身份验证 token 的方法:

    public AccountManagerFuture<Bundle> getAuthToken (Account account,
    String authTokenType, Bundle options, Activity activity,
    AccountManagerCallback<Bundle> callback, Handler handler)
  2. 我知道如何准备身份验证 XML:

    jidAndToken ="\0" + UTF8(YOURUSERNAME@gmail.com) + "\0" + Auth

    (其中“\0”是一个值为零的八位字节)。在初始 SASL 身份验证中使用它:

    <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' 
    mechanism='X-GOOGLE-TOKEN'>Base64(jidAndToken)</auth>


但我没能像有人在此处为 facebook 聊天所做的那样将它与 Smack API 集成:XMPP with Java Asmack library supporting X-FACEBOOK-PLATFORM

有人可以帮助我吗?

最佳答案

维杰,

您的代码对我帮助很大,谢谢!我在这里发帖是为了提供我对使用 AccountManager 登录 Google talk 的问题的解决方案。到目前为止,我还没有找到完整的解决方案,但我已经根据上面的代码开发了我的解决方案并更正了几行不起作用。

解决方案分为两部分。第一种是基于上面的思路和代码。就是创建一个SASLMechanism的子类:

import java.io.IOException;
import java.net.URLEncoder;

import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.sasl.SASLMechanism;

import android.util.Base64;
import android.util.Log;

public class GTalkOAuth2 extends SASLMechanism {
public static final String NAME="X-GOOGLE-TOKEN";


public GTalkOAuth2(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>" );

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

第二部分是它的使用。其他示例没有给我的重要信息是,当从 AccountManager 系统获取 token 时, token 类型不是“ah”而是“mail”。这个想法出现在与谷歌服务器直接通信以获取 token 的示例中,而不是从 AccountManager 请求它。将它们放在一起,您需要在驱动程序代码中执行以下操作。创建一个函数来获取 token :

public String getAuthToken(String name)
{
Context context = getApplicationContext();
Activity activity = this;
String retVal = "";
Account account = new Account(name, "com.google");
AccountManagerFuture<Bundle> accFut = AccountManager.get(context).getAuthToken(account, "mail", null, activity, null, null);
try
{
Bundle authTokenBundle = accFut.getResult();
retVal = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
}
catch (OperationCanceledException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (AuthenticatorException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVal;
}

然后在确保使用正确的 SASL 系统后调用它:

SASLAuthentication.registerSASLMechanism( GTalkOAuth2.NAME, GTalkOAuth2.class );
SASLAuthentication.supportSASLMechanism( GTalkOAuth2.NAME, 0 );
config.setSASLAuthenticationEnabled(true);

String saslAuthString = getAuthToken(acct.name);
connection = new XMPPConnection(config);
try {
connection.connect();
connection.login(name, saslAuthString);
} catch (XMPPException e) {
// Most likely an expired token
// Invalidate the token and start over. There are example of this available
}

Google Talking 快乐!

关于android - 如何使用 Smack API 使用 AccountManager 的身份验证 token 对 Google Talk 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7358392/

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