帮助在Python中生成Facebook API "Sig

0 投票
1 回答
1971 浏览
提问于 2025-04-15 19:17

我已经为这个问题挣扎了两天,真希望能得到你的帮助。问题是这样的:

每当我们向Facebook的REST服务器发送请求时,都需要附加一个叫“sig”的参数。这个sig是通过以下算法生成的:

<?php

$secret = 'Secret Key'; // where 'Secret Key' is your application secret key 
$args = array(
'argument1' => $argument1,
'argument2' => $argument2); // insert the actual arguments for your request in place of these example args 
$request_str = '';
foreach ($args as $key => $value) {
$request_str .= $key . '=' . $value; // Note that there is no separator.
}
$sig = $request_str . $secret;
$sig = md5($sig);

?>

关于这个的更多信息可以查看这里:http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application

我一直在尝试用Python重现这段代码,这是我的尝试:

def get_signature(facebook_parameter):
    sig = ""
    for key, value in facebook_parameter.parameters:
        sig += key + "=" + value
    sig += facebook_parameter.application_secret
    return hashlib.md5(sig).hexdigest()

facebook_paremeter.parameters是一个看起来像这样的列表:

[('api_key', '...'), ('v', '1.0'), ('format', 'JSON'), ('method', '...')]

而facebook_paremeter.application_secret是一个有效的应用密钥。

这段代码是在Google App Engine开发平台上运行的(如果这有什么影响的话)。Python版本是2.6.4。


有没有人能帮我找出我的代码哪里出错了?

谢谢,
Sri

1 个回答

1

这个列表需要被排序。

撰写回答