如何使用JavaScript SDK获取访问令牌以发布操作

时间:2012-06-11 20:06:07

标签: javascript facebook facebook-javascript-sdk

我使用以下代码通过javascript sdk获取访问令牌

<div id="fb-root"></div>
<script src="https://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

        FB.init({ 
            appId: 'insert your appID value here', 
            cookie: true, 
            xfbml: true, 
            status: true });

        FB.getLoginStatus(function (response) {
            if (response.authResponse) {
                alert('#AccessToken').val(response.authResponse.accessToken);
            } else {
                // do something...maybe show a login prompt
            }
        });

    });
</script>

现在我想使用上面代码中的访问令牌并在下面使用它来发布操作,我尝试了不同的东西,但没有一个工作。

<script type="text/javascript">
function read()
{
FB.api('/me/news.reads' +
'?article=Yoursite.com/test.html&access_token=access token from above','post',
function(response) {
var msg = 'Error occured';
if (!response || response.error) {
if (response.error) {
msg += "\n\nType: "+response.error.type+"\n\nMessage: "+response.error.message;
}
alert(msg);
}
else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
</script>

0 个答案:

没有答案