gpt4 book ai didi

c++ - facebook api过度休息得到不正确的签名 104

转载 作者:行者123 更新时间:2023-11-28 08:29:54 25 4
gpt4 key购买 nike

我正在尝试发送 rest api Users.getLoggedInUser我有所有的 secret session 和所有的认证根据这个网站: 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:看..为什么信号看起来像这样?散列 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>

最佳答案

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

QString APIMD5Signature(hashedApiData.toHex());

这可能会为您完成,但您仍然会导致来回转换为字符串的问题,而您实际上不需要这样做。

以下是我如何处理创建签名和向 Facebook 发帖的过程。 m_argMap 包含 POST 的对(例如 <"format","XML">),但 POST 不需要但 sig 需要的 secret 除外。 (由于在 QMap 到达此处之前发生了一些涉及该 QMap 的预处理,我在那里使用了 QVariant)。

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

关于c++ - facebook api过度休息得到不正确的签名<error_code> 104,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2628143/

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