views:

668

answers:

1

Hello all im trying to send rest api Users.getLoggedInUser i have all the secret session and all the Authenticationi made according to this site : http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Desktop_Applications here is my code (cpp QT but it can be any thing else ) :

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

and this is the xml im getting back: look ..why the sig looks like this ? does the hash MD5 suppose to look like set of numbers ?

<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"&gt;
  <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  A: 

The big problem is that you need to convert the hashedApiData QByteArray to Hex:

QString APIMD5Signature(hashedApiData.toHex());

That may do it for you, but you can still cause problems converting back and forth to strings, which you really don't need to do.

Here's how I handle creating the sig and doing the post to facebook. The m_argMap contains the pairs for the POST (e.g. <"format","XML">) except for the secret which isn't needed for the POST but is needed for the sig. (I'm using a QVariant there due to some pre-processing that happens involving that QMap before it gets here).

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