facebook api over rest得到错误的签名<error_code> 104 </error_code>

时间:2010-04-13 08:47:08

标签: c++ qt facebook desktop md5

我试图发送rest api Users.getLoggedInUser 我有所有秘密会话和所有Authenticationi 根据这个网站: http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Desktop_Applications 这是我的代码(cpp QT,但它可以是其他任何东西):

QString toAPISignature = 
                "api_key="
                +  API_KEY 
                + "call_id=" + strCallid 
                + "format=XML" 
                + "method=Users.getLoggedInUser"
                + "session_key="+ fbSessionKey  
                + "v=1.0"+m_fbSecretKey;

QByteArray hashedApiData= QCryptographicHash::hash(toAPISignature.toAscii(),
          QCryptographicHash::Md5);
QString APIMD5Signature(hashedApiData);


.....
.....
.....
// now set the api request
 QUrl api_url;
 api_url.setUrl(FB_REST_URL);
 api_url.addQueryItem("api_key",API_KEY);
 api_url.addQueryItem("session_key",fbSessionKey);
 api_url.addQueryItem("call_id",strCallid);
 api_url.addQueryItem("sig",APIMD5Signature);
 api_url.addQueryItem("format","XML");
 api_url.addQueryItem("method","Users.getLoggedInUser");
 api_url.addQueryItem("v","1.0");

then i init the request object :

QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
request.setRawHeader("User-Agent", "Facebook API Client 0.1");
request.setUrl(url);

do the get request :
QEventLoop loop;
QNetworkReply *reply = networkManager->get(request);
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray data=reply->readAll();

这是我回来的xml: 看..为什么sig看起来像这样? 哈希MD5是否看起来像一组数字?

<arg>
      <key>sig</key>
      <value>N{?¬???¬@?±B???????¶</value>
 </arg>

<?xml version="1.0" encoding="UTF-8"?>
<error_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
  <error_code>104</error_code>
  <error_msg>Incorrect signature</error_msg>
  <request_args list="true">
    <arg>
      <key>api_key</key>
      <value>bc5xxxxxxxxx57811</value>
    </arg>
    <arg>
      <key>session_key</key>
      <value>a99fdxxxxxxx5c2-100xxxxx455</value>
    </arg>
    <arg>
      <key>call_id</key>
      <value>1</value>
    </arg>
    <arg>
      <key>sig</key>
      <value>N{?¬???¬@?±B???????¶</value>
    </arg>
    <arg>
      <key>format</key>
      <value>XML</value>
    </arg>
    <arg>
      <key>method</key>
      <value>Users.getLoggedInUser</value>
    </arg>
    <arg>
      <key>v</key>
      <value>1.0</value>
    </arg>
  </request_args>
</error_response>

1 个答案:

答案 0 :(得分:1)

最大的问题是你需要将hashedApiData QByteArray转换为Hex:

QString APIMD5Signature(hashedApiData.toHex());

这可能会为你做,但你仍然可能导致来回转换为字符串的问题,你真的不需要这样做。

这是我如何处理创建信号和发布到Facebook的帖子。 m_argMap包含POST的对(例如&lt;“format”,“XML”&gt;),除了POST不需要但秘密需要的秘密。 (我在那里使用了一个QVariant,因为在它到达之前会发生涉及QMap的一些预处理。)

QByteArray sigByteArray;
QByteArray postArgs;

// QMap is automatically sorted by keys
QMapIterator<QString, QVariant> i(m_argMap);
while (i.hasNext()) {
     i.next();
     sigByteArray.append(i.key() + "=" + i.value().toString() );
     postArgs.append(i.key() + "=" + i.value().toString() + "&");
}

sigByteArray.append(m_userInfo->getSecret());

QByteArray sig = QCryptographicHash::hash(sigByteArray,QCryptographicHash::Md5 );

postArgs.append("sig=");
postArgs.append(sig.toHex());

QByteArray exclude("&=");
QByteArray include;
postArgs = postArgs.toPercentEncoding(exclude,include,'%');

// qDebug() << postArgs;

QUrl url("http://api.facebook.com/restserver.php");

QNetworkRequest nr;
nr.setUrl(url);
nr.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

m_reply = m_manager->post(nr,postArgs);