Friday, January 13, 2012

Posting on facebook wall from Qt

Recently I was working on upgrade of my application. As a part of upgrade I decided to allow user to post their game score to facebook wall.

I had done similar work for iPhone, here is link. But never from Qt.

I am sharing my experience here, in hope it will help someone.

To be able to post on wall first you need to create facebook application. You can do the from here.

After creating application, you will get application id. We need this application id for authorization.

We need to use following url for authorization.

In below request client_id is your facebook application id, score is required permission to call facebook graph API. for posting on user's wall read_stream and publish_stream is enough. Here is full list of permission.
Above url will offer user a login dialog and user needs to enter user name and password. if process is successful then we will get access token. I used QWebView for presenting dialog to user.

You can also use "" for mobile version of login dialog.

Following is my Qt Code.
void FacebookHelper::login( const QString& scope ){

    QUrl url("");

    //view = new QWebView();
    view->load( url );

void FacebookHelper::loginResponse( bool status ){
    QUrl url= view->url();
    QString strUrl = url.toString();

    int sIndex = strUrl.indexOf("access_token=");
    int eIndex = strUrl.indexOf("&expires_in");
    if( sIndex != -1 && eIndex != -1 ){
        mAccessToken= strUrl.mid(sIndex, eIndex - sIndex);
        mAccessToken = mAccessToken.remove("access_token=");
        emit authStatus( mAccessToken);
Access token will come as part of re directional url, like following.
Now we have access token, Using this token we can submit post to facebook wall.
You can use following curl request to test your access token and post a message to your facebook wall.

If you see post you your wall then authorization process is successful. Here, url is used to post message on logged in user's wall. If you want to post message on friends post then you need to replace me with that user's id.
 curl -F 'access_token=ACCESS_TOKEN' \
     -F 'picture=URL OF SOME PICTURE' \
     -F 'caption=TITLE OF POST' \
     -F 'description=DETAILS OF POST' \
     -F 'name=NAME OF POST' \
Following is my Qt code for sending message to user's wall.

        QMap data;
        data["access_token"] = mAccessToken;
        data["name"] = "Test ";
        data["caption"] = "Testing graph api";
        data["description"] = "Testing description field";
        data["picture"] = "URL";

void FacebookHelper::setMessage( QMap<qstring,qstring>& post) {

    QString postData;
    foreach( QString key, post.keys()) {
        postData.append(key +"=" +post[key]+"&");

    QNetworkRequest request(QUrl("");
    mCurrentRequest  =,postData.toAscii());
Thant's all, Hope you liked it.


  1. Don't work for me. Maybe the postData must be encoded ...

  2. I made the same thing in java using http url , but nothing appear in facebook feed. I'm behind a proxy server; I set the app proxy and the same result: doesn't work. I tried to compile the app for desktop ... no result.

  3. I will suggest you to try your request with curl first, that way you can decide problem is with program or http request.

  4. an active acces token must be used to quey information about the current user ....

    I have a valid access token

    P.S. I used curl for win x64

  5. my bad was " instead of '

    how can I include open ssl to my project ?

  6. I checked if my app support ssl QSslSocket::supportsSsl();

    but the same result: no post, no answer

  7. What Capabilities you use ?

  8. can you try to use wireshark, to check what is happening at network packet level ? "What Capabilities you use ?" regarding what, I did not understand your question ?

  9. This comment has been removed by a blog administrator.

  10. This is my first try with facebook client app. I just wonder if I know user's email and passoword then can I post message to the facebook user's wall directly? In case I don't want to show the FaceBook authentication page. Is this possible?

    1. Hi, I dont think that its possible, at lest not when i tried. Now I am not sure if any update cause any changes.

    2. It IS possible. But is VERY hard. Even facebook dev pages don't recommend you to do that, because the OAuth2 (the facebook authentication protocol) is for web only, not desktop.

  11. Can you make a zip file or a tar with the full code? Thanks!

    1. have a look here,

      It's same code, I used for some competition, There is source code attached at bottom.