使用Javascript sdk在Facebook墙上发布

时间:2012-12-10 22:19:11

标签: facebook facebook-javascript-sdk

所以,我正试图使用​​fb.api在用户墙上发帖,我被卡住了。 这是我的代码:

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

这里的问题是我收到此错误代码:必须使用活动访问令牌来查询有关当前用户的信息。 即使我使用登录按钮获取代码,我也明白了。是否有可能在函数postToWallUsingFBApi()中添加以前获得的访问令牌。我可以更改/ me / with用户ID,这样用户可以注销并仍然发布吗?

1 个答案:

答案 0 :(得分:3)

如果您以这种方式进行客户端登录,则必须自己从URL中提取访问令牌 - 请参阅https://developers.facebook.com/docs/howtos/login/client-side-without-js-sdk/#step3

或者你只是使用JS SDK中的FB.login方法 - 稍微容易一点,处理所有必要的“开箱即用” - https://developers.facebook.com/docs/howtos/login/getting-started/#step4