首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用X- facebook -PLATFORM SASL身份验证的FACEBOOK聊天身份验证

使用X- facebook -PLATFORM SASL身份验证的FACEBOOK聊天身份验证
EN

Stack Overflow用户
提问于 2011-05-21 00:29:08
回答 2查看 4.5K关注 0票数 2

我正在使用X- facebook -PLATFORM SASL身份验证机制处理FACEBOOK聊天身份验证。

我按照facebook开发者论坛和stackoverflow问题中所解释的那样形成用户和密码。

关键是,如果我使用application_secret作为密码,我就可以登录,但根据堆栈溢出问题(下面的链接),它应该是从旧的rest api方法auth.promoteSession生成的会话

我想使用旧的rest api方法,以避免在我们的桌面应用程序jars中分发application_secret。

所以问题是,你是如何用auth.promoteSession登录的?

我已经阅读了以下帖子,它们对我有很大的帮助:

http://community.igniterealtime.org/message/205739#205739

XMPP with Java Asmack library supporting X-FACEBOOK-PLATFORM

我使用的是SASLXFacebookPlatformMechanism.java类,它来自于igniterealtime帖子,并且注册正确。

我有xmpp_login和offline_access权限。而且我已经禁用了Remove Deprecated方法,所以我可以调用旧的rest api方法,在本例中: auth.promoteSession我也在使用客户端对facebook进行身份验证。

所以,使用application_secret作为我得到的密码:

代码语言:javascript
复制
<stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-FACEBOOK-PLATFORM</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms></stream:features>
<challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dmVyc2lvbj0xJm1ldGhvZD1hdXRoLnhtcHBfbG9naW4mbm9uY2U9NEIxRUQzNTA5MTQ5MDQxRTE4N0QyNTA0NTUzNjBDQjc=</challenge>
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>

但是如果我使用由我得到的auth.promoteSession返回的值:

代码语言:javascript
复制
<stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-FACEBOOK-PLATFORM</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms></stream:features>
<challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dmVyc2lvbj0xJm1ldGhvZD1hdXRoLnhtcHBfbG9naW4mbm9uY2U9MzhFQkUxOTUxNENGRUU4ODc2NzRDREQ0RjhBMUQ0QjI=</challenge>
<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
EN

回答 2

Stack Overflow用户

发布于 2011-09-13 13:51:24

我已经更改了Android的版本,它现在适用于我了

代码语言:javascript
复制
public class SASLXFacebookPlatformMechanism extends SASLMechanism {

    private static final String NAME              = "X-FACEBOOK-PLATFORM";

    private String              apiKey            = "";
    private String              accessToken        = "";

    /**
     * Constructor.
     */
    public SASLXFacebookPlatformMechanism(SASLAuthentication saslAuthentication) {
        super(saslAuthentication);
    }

    @Override
    protected void authenticate() throws IOException, XMPPException {
        getSASLAuthentication().send(new AuthMechanism(NAME, ""));
    }

    @Override
    public void authenticate(String apiKey, String host, String accessToken) throws IOException, XMPPException {
        if (apiKey == null || accessToken == null) {
            throw new IllegalArgumentException("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 decodedChallenge = new String(Base64.decode(challenge));
            Map<String, String> parameters = getQueryMap(decodedChallenge);

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

            String composedResponse =
                "method=" + URLEncoder.encode(method, "utf-8") +
                        "&nonce=" + URLEncoder.encode(nonce, "utf-8") +
                        "&access_token=" + URLEncoder.encode(accessToken, "utf-8") +
                        "&api_key=" + URLEncoder.encode(apiKey, "utf-8") +
                        "&call_id=0" +
                        "&v=" + URLEncoder.encode(version, "utf-8");
            response = composedResponse.getBytes();
        }

        String authenticationText = "";

        if (response != null) {
            authenticationText = Base64.encodeBytes(response);
        }

        // Send the authentication to the server
        getSASLAuthentication().send(new Response(authenticationText));
    }

    private Map<String, String> getQueryMap(String query) {
        Map<String, String> map = new HashMap<String, String>();
        String[] params = query.split("\\&");

        for (String param : params) {
            String[] fields = param.split("=", 2);
            map.put(fields[0], (fields.length > 1 ? fields[1] : null));
        }

        return map;
    }
}

此版本仅需要应用程序id和访问令牌

代码语言:javascript
复制
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
mFbConnection = new XMPPConnection(config);

try {
    SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
    SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
    mFbConnection.connect();
    mFbConnection.login(apiKey, accessToken, "Application");
} catch (XMPPException e) {
    mFbConnection.disconnect();
    e.printStackTrace();
}

我希望这能有所帮助。

票数 6
EN

Stack Overflow用户

发布于 2011-08-17 22:17:17

是的,对我来说,你两者都需要。来自XMPP with Java Asmack library supporting X-FACEBOOK-PLATFORM的代码需要调整,以包括应用程序秘密以及会话秘密(作为密码)。

代码语言:javascript
复制
this.apiKey = keyArray[0];
    Log.d("API_KEY", apiKey);
    this.applicationSecret = "################################";
    Log.d("SECRET_KEY", applicationSecret);
    this.sessionKey = keyArray[1];
    Log.d("SESSION_KEY", sessionKey);

    this.authenticationId = sessionKey;
    this.password = applicationSecret;
    this.hostname = host;

为你的appSecret换出######################## (在你的开发区)

这一点在文档或IMO帖子中并不清楚。会话密钥是通过FB.getSession()获得的,但也可以使用其他选项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6074940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档