Saturday, February 26, 2022

[FIXED] Facebook JS API errors

Issue

I am trying to create a test application the source is the following (index.php):

<?php
include_once 'facebook/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YYYYYYYYYYYYYYYY',
  'secret' => 'XXXXXXXXXXXXXXXXXXXXXX',
));

$user = $facebook->getUser();

if($user)
{
  try
  {
    $user_profile = $facebook->api('/me');
  }
  catch(FacebookApiException $e)
  {
    error_log($e);
    $user = null;
  }
}

if( ! $user)
{
  echo "<script type=\"text/javascript\">top.location.href='" . $facebook->getLoginUrl(array(
    'scope' => 'publish_actions,publish_stream',
    'redirect_uri' => 'http://apps.facebook.com/MYAPPURL/'
  )) . "'</script>";
  exit;
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>TribusWar</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="http://connect.facebook.net/en_US/all.js#appId=YYYYYYYYYYYYYYYY&amp;xfbml=1"></script>
</head>
<body>
<input type="button" value="Compartilhar" id="xxxxx" />
</body>
<script type="text/javascript">
document.getElementById('xxxxx').addEventListener('click', function()
{
  FB.ui({
    method: 'feed',
    display: 'iframe',
    name: 'Dialog Name',
    caption: 'Caption for dialog',
    description: 'Lorem ipsum dolor sit amet...'
  }, function()
  {
    alert(arguments);
  });
}, false);
</script>
</html>

With this code, I wanted to show an button on the page which fires an publish dialog when clicked. When the page loads, it generates an error, when the button is clicked, another error occurs and the dialog is not shown:

Firebug Console

I don't know what is going wrong with this. I don't know if the publish_stream permission is really necessary as I am trying to use the facebook dialog. Can anyone help-me?


Solution

As Nava Salvatore (and the debugger console) says, you need to add:

<div id="fb-root"></div>

somewhere within the <body> of your code. Additionally, your final <script> block should be before the </body> tag (per convention). You don't need any additional permissions for simply displaying a dialog; publish_stream is not needed.



Answered By - Jimmy Sawczuk

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.