使用Javascript sdk在Facebook墙上发布

使用Javascript sdk在Facebook墙上发布,facebook,facebook-javascript-sdk,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>Exa

所以,我试图用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>

使用Javascript图形API发布到墙的示例
//facebook sdk包括
var APP_ID=“myAppID”;
window.fbasyninit=init;
函数initFacebook()
{
FB.init({
appId:APP_ID,
状态:true,//检查登录状态
cookie:false,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
FB.getLoginStatus(onFacebookLoginStatus);
};
(功能(){
var e=document.createElement('script');
e、 src=document.location.protocol+'//connect.facebook.net/en_US/all.js';
e、 异步=真;
document.getElementById('fb-root').appendChild(e);
}());
//登录功能
函数facebookLogin()
{
变量loginUrl=”http://www.facebook.com/dialog/oauth/?"+
“范围=发布\u流&”+
“客户端id=“+应用程序id+”&”+
“重定向_uri=“+document.location.href+”&”+
“响应类型=令牌”;
window.location=loginUrl;
}
//FB.login的回调函数
函数onFacebookLoginStatus(响应)
{
if(response.status==“connected”&&response.authResponse)
{
document.getElementById(“txtEcho”).innerHTML=“已登录。”;
}
其他的
{
document.getElementById(“txtEcho”).innerHTML=“未登录。”;
}
}
//墙后功能
函数PostToAllusingFBAPI()
{
var数据=
{
描述:“这是我的墙贴示例”,
消息:“使用FB.api发布”,
链接:'http://wwww.permadi.com/blog/',
}
FB.api('/me/feed',post',data,onposttowall完成);
}
//过帐到墙后的返回功能
功能OnPostToWall已完成(响应)
{
如果(答复)
{
if(response.error)
{
document.getElementById(“txtEcho”).innerHTML=response.error.message;
}
其他的
{
if(response.id)
document.getElementById(“txtEcho”).innerHTML=“发布为post_id”+response.id;
else if(响应.post_id)
document.getElementById(“txtEcho”).innerHTML=“作为post\u id发布”+response.post\u id;
其他的
document.getElementById(“txtEcho”).innerHTML=“未知错误”;
}
}
}
这里的问题是我收到了这个错误代码:必须使用活动访问令牌来查询有关当前用户的信息。
即使我使用“登录”按钮获取代码,我也会得到它。是否可以在函数posttoAllusingFBAPI()中添加以前获得的访问令牌。我可以用用户id更改/me/吗,这样用户就可以注销并仍然发布?

如果您以这种方式进行客户端登录,您必须自己从URL中提取访问令牌–请参阅

或者,您只需使用JS SDK中的FB.login方法即可–更简单一些,可以“开箱即用”地处理所有必要的内容-