Page 1 of 1

Facebook: Eine falsche Signatur (104) beim Erhalten von Sitzungsschlüssel erhalten

Posted: 18 Aug 2025, 23:08
by Anonymous
Ich versuche, die HTTPClient -Bibliothek (um die REST -Endpunkte der Facebook -API aufzurufen), um einen Sitzungsschlüssel zu erhalten und Benutzer zu überprüfen ... < /p>

Mein Code ist hier: < /p>

RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{ "classpath*:**/*applicationContext-test.xml" })
public class FacebookUserTest
{
final String API_KEY = "9e2568d68f182a2957878f3acedd9453";
final String SECRET = "f1298956895d39110be92a672e0d2284";
final String TOKEN = "aa0c41aa4532053e8d0097844ab9bc7d";
final String LOGIN = "http://www.facebook.com/login.php";
final String HOST = "http://www.facebook.com/restserver.php";

private static Logger log =
LoggerFactory.getLogger(FacebookUserTest.class);

protected FacebookUser FacebookUser;

@Test
public void testFindUserBySessionKey() throws Exception
{
loginToFacebook();
String sessionKey = getSessionKey();
}

public void loginToFacebook() throws Exception
{
HttpClient client = new HttpClient();
client.setParams(new HttpClientParams());
client.setState(new HttpState());

GetMethod get = new GetMethod(LOGIN + "?api_key=" + API_KEY
+ "&v=1.0&auth_token=" + TOKEN);

// Get login screen
client.executeMethod(get);

// Post credentials to login
PostMethod post = new PostMethod(LOGIN);
post.addParameter(new NameValuePair("api_key", API_KEY));
post.addParameter(new NameValuePair("v", "1.0"));
post.addParameter(new NameValuePair("auth_token", TOKEN));
post.addParameter(new NameValuePair("email", "[email protected]"));
post.addParameter(new NameValuePair("pass", "password"));
client.executeMethod(post);
}

public String getSessionKey() throws Exception
{
HttpClient client = new HttpClient();

// Obtain session key
String host = "http://www.facebook.com/restserver.php";
String sessSecret = "false";
String toMd = "api_key=" + API_KEY + "auth_token=" + TOKEN
+ "format=xmlgenerate_session_secret=" + sessSecret
+ "method=facebook.auth.getSessionv=1.0" + SECRET;
String md5 = Md5Utils.MD5(toMd);

PostMethod post = new PostMethod(HOST);

NameValuePair[] data = {
new NameValuePair("api_key", API_KEY),
new NameValuePair("auth_token", TOKEN),
new NameValuePair("format", "xml"),
new NameValuePair("generate_session_secret", SECRET),
new NameValuePair("method", "auth.getSession"),
new NameValuePair("sig", md5), new NameValuePair("v", "1.0")
};

post.setRequestBody(data);
post.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
post.setRequestHeader("User-Agent",
"Facebook API PHP5 Client 1.1 (curl) 5");

// execute method and handle any error responses.
client.executeMethod(post);

StringBuilder sb = new StringBuilder();
byte[] b = new byte[4096];

for (int n; (n = post.getResponseBodyAsStream().read(b)) != -1;)
{
sb.append(new String(b, 0, n));
}

String sessionId = sb.toString();
log.warn("Session Id: " + sessionId);

return sessionId;
}
}
< /code>

Wenn ich meinen Junit -Test ausführe, wird dies aus der Konsole gedruckt: < /p>

WARN : com.myapp.FacebookUserTest - Session Id:



104
Incorrect signature


api_key
9e2568d68f182a2957878f3acedd9453


auth_token
aa0c41aa4532053e8d0097844ab9bc7d


format
xml


generate_session_secret
f1298956895d39110be92a672e0d2284


method
auth.getSession


sig
fcf80d658f35d66396ac521da7102782


v
1.0



< /code>

Könnte es mein Md5utils -Code sein: < /p>

public class Md5Utils
{
private static String convertToHex(byte[] data)
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0