Lexx
Lexx

Reputation: 725

Post on Facebook wall with Javascript sdk

So, I'm trying to post on users wall using fb.api and I'm stuck. Here is my code:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"><head><meta http-           equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Example Of Posting To Wall Using Javascript Graph API</title>
<script type="text/javascript" src=""></script>
    </head>
    <body class="">
    //the facebook sdk include
    <div id="fb-root" class=" fb_reset">
    <script>
var APP_ID="myAppID";

window.fbAsyncInit = initFacebook;

function initFacebook()
{
    FB.init({
      appId  : APP_ID,
      status : true, // check login status
      cookie : false, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });

    FB.getLoginStatus(onFacebookLoginStatus);
};

(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
    }());
    //the login function
function facebookLogin()
{
    var loginUrl="http://www.facebook.com/dialog/oauth/?"+
        "scope=publish_stream&"+
        "client_id="+APP_ID+"&"+
        "redirect_uri="+document.location.href+"&"+
        "response_type=token";
    window.location=loginUrl;
}


//Callback function for FB.login
function onFacebookLoginStatus(response)
{
    if (response.status=="connected" && response.authResponse)
    {
        document.getElementById("txtEcho").innerHTML="Logged in.";

    }
    else
    {
        document.getElementById("txtEcho").innerHTML="Not logged in.";
    }

}


    //post to wall function
function postToWallUsingFBApi()
{
    var data=
    {
        caption: 'This is my wall post example',
        message: 'Posted using FB.api',
        link: 'http://wwww.permadi.com/blog/',
     }
    FB.api('/me/feed', 'post', data, onPostToWallCompleted);
}

    //the return function after posting to wall
function onPostToWallCompleted(response)
{
    if (response)
    {
        if (response.error)
        {
            document.getElementById("txtEcho").innerHTML=response.error.message;
        }
        else
        {
            if (response.id)
                document.getElementById("txtEcho").innerHTML="Posted as post_id "+response.id;
            else if (response.post_id)
                document.getElementById("txtEcho").innerHTML="Posted as post_id "+response.post_id;
            else
                document.getElementById("txtEcho").innerHTML="Unknown Error";
        }
    }
}



    </script>
    <input id="loginButton" type="button" value="Login To Facebook" onclick="javascript:facebookLogin();">
    <input id="postToWallWithFBApiPrompt" type="button" value="Post To Wall Using FB.api"          onclick="javascript:postToWallUsingFBApi();">
    <div id="txtEcho"><b></b></div>
    </body>
    </html>

The problem here is that I receive this error code: An active access token must be used to query information about the current user. I get that even if I use the log in button to get the code. Is there a possibility to add previously obtained access token in function postToWallUsingFBApi(). And can I change the /me/ with user id, so that way the user can be logged out and still post?

Upvotes: 2

Views: 20475

Answers (1)

C3roe
C3roe

Reputation: 96151

If you are doing client-side login that way, you have to extract the access token from the URL yourself afterwards – see https://developers.facebook.com/docs/howtos/login/client-side-without-js-sdk/#step3

Or you just use the FB.login method out of the JS SDK – a little easier, handles all the ecessary stuff “out of the box” – https://developers.facebook.com/docs/howtos/login/getting-started/#step4

Upvotes: 3

Related Questions