Xmpp X-FACEBOOK-PLATFORM身份验证错误

Xmpp X-FACEBOOK-PLATFORM身份验证错误,xmpp,x-facebook-platform,Xmpp,X Facebook Platform,我正在尝试使用下面的代码通过SMACK连接到Facebook聊天api,但我总是收到X-Facebook-PLATFORM失败:未授权。我有一个xmpp许可的有效令牌,但我想我遗漏了一些东西。。。它使用user/pass,但不使用accessToken 谢谢你们的帮助,伙计们,这件事快把我逼疯了 public class FacebookChatSample { public static void main( ) { String accessToken = //interne

我正在尝试使用下面的代码通过
SMACK
连接到
Facebook聊天api
,但我总是收到
X-Facebook-PLATFORM失败:未授权
。我有一个xmpp许可的有效令牌,但我想我遗漏了一些东西。。。它使用user/pass,但不使用accessToken

谢谢你们的帮助,伙计们,这件事快把我逼疯了

public class FacebookChatSample {

public static void main(   )
{

    String accessToken = //internet example "AAACjg0Eh1N8BAFuhUFZAN0EteV6pjZAsZAI46i8oV3iVmyLdaKiwaBcM5DPFjbEZAq9LZAZAA0qXsvaXkKB7SqnhubzlUAK7tmr3UYLaeQgaXSJ6EZCKn2G";
    String consumerKey = //internet example "179784128779487";

    long targetFacebookId = 550121201669650L;

    String toID = "1002221765";

    String message = "HELLOOOOO FROM MY JAVA PROGRAM!!";

    XMPPConnection connection = createXMPPConnection();
    try
    {
        connection.connect();
        connection.login( consumerKey, accessToken );

        String to = String.format( "-%d@chat.facebook.com", Long.valueOf( targetFacebookId ) );
        Chat chat = connection.getChatManager().createChat( to, null );
        chat.sendMessage( message );
    }
    catch( XMPPException e )
    {
       e.printStackTrace();
    }
    finally
    {
        connection.disconnect();
    }
}

private static synchronized XMPPConnection createXMPPConnection()
{
    SASLAuthentication.registerSASLMechanism(
            SASLXFacebookPlatformMechanism.NAME,
            SASLXFacebookPlatformMechanism.class );
    SASLAuthentication.supportSASLMechanism(
            SASLXFacebookPlatformMechanism.NAME, 0 );

    ConnectionConfiguration configuration = new ConnectionConfiguration(
            "chat.facebook.com", 5222 );
    configuration.setSASLAuthenticationEnabled( true );

    return new XMPPConnection( configuration );
}

public static class SASLXFacebookPlatformMechanism extends SASLMechanism
{
    public static final String NAME = "X-FACEBOOK-PLATFORM";

    public SASLXFacebookPlatformMechanism(
            SASLAuthentication saslAuthentication )
    {
        super( saslAuthentication );
    }

    private String apiKey = "";

    private String accessToken = "";

    @Override
    protected void authenticate() throws IOException, XMPPException
    {
        AuthMechanism stanza = new AuthMechanism( getName(), null );
        getSASLAuthentication().send( stanza );
    }

    @SuppressWarnings( "hiding" )
    @Override
    public void authenticate( String apiKey, String host, String accessToken )
            throws IOException, XMPPException
    {
        if( apiKey == null || accessToken == null )
        {
            throw new IllegalStateException( "Invalid parameters!" );
        }

        this.apiKey = apiKey;
        this.accessToken = accessToken;
        this.hostname = host;

        String[] mechanisms = { "DIGEST-MD5" };
        Map<String, String> props = new HashMap<String, String>();
        this.sc = Sasl.createSaslClient( mechanisms, null, "xmpp", host,
                props, this );
        authenticate();
    }

    @Override
    public void authenticate( String username, String host,
            CallbackHandler cbh ) throws IOException, XMPPException
    {
        String[] mechanisms = { "DIGEST-MD5" };
        Map<String, String> props = new HashMap<String, String>();
        this.sc = Sasl.createSaslClient( mechanisms, null, "xmpp", host,
                props, cbh );
        authenticate();
    }

    @Override
    protected String getName()
    {
        return NAME;
    }

    @Override
    public void challengeReceived( String challenge ) throws IOException
    {
        byte response[] = null;
        if( challenge != null )
        {
            String decodedResponse = new String(
                    org.jivesoftware.smack.util.Base64.decode( challenge ) );
            Map<String, String> parameters = getQueryMap( decodedResponse );

            String version = "1.0";
            String nonce = parameters.get( "nonce" );
            String method = parameters.get( "method" );

            Long callId = Long.valueOf( System.currentTimeMillis() );

            String composedResponse = String
                    .format(
                            "method=%s&nonce=%s&access_token=%s&api_key=%s&call_id=%s&v=%s",
                            URLEncoder.encode( method, "UTF-8" ),
                            URLEncoder.encode( nonce, "UTF-8" ),
                            URLEncoder.encode( this.accessToken, "UTF-8" ),
                            URLEncoder.encode( this.apiKey, "UTF-8" ),
                            callId, URLEncoder.encode( version, "UTF-8" ) );
            response = composedResponse.getBytes();
        }

        String authenticationText = "";

        if( response != null )
        {
            authenticationText = org.jivesoftware.smack.util.Base64
                    .encodeBytes(
                            response,
                            org.jivesoftware.smack.util.Base64.DONT_BREAK_LINES );
        }

        Response stanza = new Response( authenticationText );

        getSASLAuthentication().send( stanza );
    }

    private Map<String, String> getQueryMap( String query )
    {
        String[] params = query.split( "&" );
        Map<String, String> map = new HashMap<String, String>();
        for( String param : params )
        {
            String name = param.split( "=" )[0];
            String value = param.split( "=" )[1];
            map.put( name, value );
        }
        return map;
    }
}
}
公共类FacebookChatSample{
公共静态void main()
{
String accessToken=//互联网示例“aaacjg0eh1n8bafuhufzan0etev6pjzaszai46i8ov3ivmyldakiwabcm5dpfjbezaq9lzaa0qxsvaxkb7sqnhubzluak7tmr3uylaeqgaxsj6ezckn2g”;
字符串consumerKey=//互联网示例“179784128779487”;
长targetFacebookId=550121201669650L;
字符串toID=“100221765”;
String message=“Helloooo来自我的JAVA程序!!”;
XMPPConnection=createXMPPConnection();
尝试
{
connection.connect();
登录(consumerKey、accessToken);
String to=String.format(“-%”d@chat.facebook.com“,Long.valueOf(targetFacebookId));
Chat Chat=connection.getChatManager().createChat(to,null);
发送消息(message);
}
捕获(XMPPException e)
{
e、 printStackTrace();
}
最后
{
连接断开();
}
}
私有静态同步XMPPConnection createXMPPConnection()
{
SASLAuthentication.registerSASLMechanism(
SASLXFacebookPlatformMechanism.NAME,
SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism(
SASLXFacebookPlatformMechanism.NAME,0);
ConnectionConfiguration配置=新连接配置(
“chat.facebook.com”,5222);
configuration.setaslauthenticationenabled(true);
返回新的XMPPConnection(配置);
}
公共静态类SASLXFacebookPlatformMechanism扩展了SASLMechanism
{
公共静态最终字符串NAME=“X-FACEBOOK-PLATFORM”;
公共图书平台机制(
萨斯劳认证(萨斯劳认证)
{
超级(saslAuthentication);
}
私有字符串apiKey=“”;
私有字符串accessToken=“”;
@凌驾
受保护的void authenticate()引发IOException,xmppeException
{
AuthMechanism节=新的AuthMechanism(getName(),null);
getSASLAuthentication().send(节);
}
@抑制警告(“隐藏”)
@凌驾
公共无效身份验证(字符串apiKey、字符串主机、字符串accessToken)
抛出IOException,xmppeException
{
if(apiKey==null | | accessToken==null)
{
抛出新的IllegalStateException(“无效参数!”);
}
this.apiKey=apiKey;
this.accessToken=accessToken;
this.hostname=host;
字符串[]机制={“摘要-MD5”};
Map props=newhashmap();
this.sc=Sasl.createSaslClient(机制,null,“xmpp”,主机,
道具,这个);
验证();
}
@凌驾
公共无效身份验证(字符串用户名、字符串主机、,
CallbackHandler(cbh)抛出IOException、XMPPException
{
字符串[]机制={“摘要-MD5”};
Map props=newhashmap();
this.sc=Sasl.createSaslClient(机制,null,“xmpp”,主机,
道具(cbh);
验证();
}
@凌驾
受保护的字符串getName()
{
返回名称;
}
@凌驾
public void challengeReceived(字符串质询)引发IOException
{
字节响应[]=null;
if(质询!=null)
{
String decodedResponse=新字符串(
org.jivesoftware.smack.util.Base64.decode(challenge));
映射参数=getQueryMap(decodedResponse);
字符串version=“1.0”;
字符串nonce=parameters.get(“nonce”);
String方法=parameters.get(“方法”);
Long callId=Long.valueOf(System.currentTimeMillis());
String composedResponse=String
.格式(
“方法=%s&nonce=%s&access\u令牌=%s&api\u密钥=%s&call\u id=%s&v=%s”,
URLEncoder.encode(方法“UTF-8”),
URLEncoder.encode(nonce,“UTF-8”),
URLEncoder.encode(this.accessToken,“UTF-8”),
URLEncoder.encode(this.apiKey,“UTF-8”),
callId,URLEncoder.encode(版本,“UTF-8”);
response=composedResponse.getBytes();
}
字符串authenticationText=“”;
if(响应!=null)
{
authenticationText=org.jivesoftware.smack.util.Base64
.encodeBytes(
答复,,
org.jivesoftware.smack.util.Base64.don_BREAK_line);
}
响应节=新响应(authenticationText);
getSASLAuthentication().send(节);
}
私有映射getQueryMap(字符串查询)
{
字符串[]params=query.split(&);
Map Map=newhashmap();
for(字符串参数:params)
{
字符串名称=参数拆分(“=”[0];
字符串值=参数拆分(“=”[1];
map.put(名称、值);
}
返回图;
}
}
}

我的天啊,我也一直在为此奋斗。我刚想出来!一旦我得到它,它看起来很容易,但令人困惑的是,XMPP的问题实际上是Facebook的问题。我认为您的问题(我的问题是)Facebook要求您请求与XMPP协议无关的特定于应用程序的登录权限。如果您查看聊天API规范,它会说如果您使用X,您需要为您的应用程序请求xmpp_登录扩展权限
session.requestNewReadPermissions(new NewPermissionsRequest(getActivity(), Arrays.asList("xmpp_login")));