开发者

Problem getting FB JS SDK to work on FB canvas app

开发者 https://www.devze.com 2023-01-07 10:36 出处:网络
I am migrating my Facebook canvas application to using the new PHP SDK. However, I am having a problem getting the JavaScript SDK to work too.

I am migrating my Facebook canvas application to using the new PHP SDK. However, I am having a problem getting the JavaScript SDK to work too.

I'm wanting to take advantage of methods such as stream.publish in the JavaScript SDK. Unfortunately I've not been able to get anything to work thus far. I have the following in the header of my application's index.php file:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_GB/all.js"></script>
<script>
    FB.init({appId: *);
    FB.ui({
        method: 'stream.publish',
        message: 'Check out this great app!'
    });
</script>

My application ID 开发者_JS百科has been removed for obvious reasons.

Where I'd expect the above to give the user a prompt to publish a message to their stream, it's not. Can I even use the new JavaScript SDK in Facebook canvas applications? Or is it reserved for iFrame applications and utilizing Facebook Connect on websites only?


Sorry, you can't use JavaScript SDK or even pure JavaScript in FBML canvas apps. Facebook created their own replacement of JavaScript called FBJS with limited functionality (for security reasons). So in canvas app you can use only PHP SDK, FBML and FBJS. In order to publish on the wall you need to use PHP SDK probably.


Make sure you're building a IFrame Canvas application, and not a FBML application. The JavaScript SDK only works with IFrames. Here's an example of stream.publish on Canvas: http://apps.facebook.com/fbrelll/fb.ui/stream.publish


I've solved it using the following:

<?php
$attachment = array(
    'name' => 'Attachment name here',
    'href' => 'Attachment URL here',
    'media' => array(array(
        'type' => 'image',
        'src'  => 'Image URL here',
        'href' => 'Link URL here',
    )),
    'caption' => 'Caption here',
);
$action_links = array(array(
    'text' => 'Action link text',
    'href' => 'Action link href',
));
?>
<script type="text/javascript">
<!--
var message = "Your status update here...";
var attachment = <?php echo json_encode($attachment); ?>;
var action_links = <?php echo json_encode($action_links); ?>;
Facebook.streamPublish(message, attachment, action_links);
//-->
</script>

Thanks to all that answered.

0

精彩评论

暂无评论...
验证码 换一张
取 消