gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:57:14 24 4
gpt4 key购买 nike

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

我正在按照 Facebook 开发者论坛和 stackoverflow 问题中的说明形成用户和密码。

重点是,如果我使用 application_secret 作为密码,我可以登录,但根据 stackoverflow 问题(下面的链接),它应该是从旧的 Rest api 方法 auth.promoteSession 生成的 session

我想使用旧的rest api方法,以避免在我们的桌面应用程序jar中分发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权限。我已经禁用了“删除不推荐使用的身份验证方法”,因此我可以调用旧的其余 api 方法,在本例中为: auth.promoteSession我也在 Facebook 中使用客户端流身份验证。

因此,使用 application_secret 作为密码我得到:

<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 返回的值,我会得到:

<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>

最佳答案

我已经更改了 Android 版本,现在可以使用了

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 和访问 token

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

我希望这会有所帮助。

关于java - 使用 X-FACEBOOK-PLATFORM SASL 身份验证的 facebook 聊天身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074940/

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