0
votes

I'm new in facebook apps development and I'm get stuck on how to post to facebook. I've read tutorials, and do according to them.

Here is the code :

<?php

require 'facebook.php';
// Create our Application instance (replace this with your appId and secret).

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

$facebook = new Facebook(array(
  'appId' => 'xxx',
  'secret' => 'xxx',
  'cookie' => true,
));


//Request params
if(!($_GET['code'])) {
    header("Location:https://www.facebook.com/dialog/oauth?client_id=xxx&redirect_uri=" . urlencode("http://localhost/facebook/examples/") . "&scope=publish_stream");
    //header("Location:http://www.google.com");
     exit;
} 

$token = $_GET['code'];
echo "token " . $token


$status = $facebook->api('/me/feed', 'POST', array('message' => 'This post came from my app.', 'access_token' => $token));
var_dump($status);

?>

Result:
Fatal error: Uncaught OAuthException: Bad signature thrown in C:\wamp\www\facebook\examples\facebook.php on line 543

Did I miss something?

1

1 Answers

0
votes

Ok, you are using raw OAuth login and at the same time using facebook php SDK. Don't do it like that, if you are using SDK us it all the way:

<?php

require 'facebook.php';
// Create our Application instance (replace this with your appId and secret).

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

$facebook = new Facebook(array(
  'appId' => 'xxx',
  'secret' => 'xxx',
  'cookie' => true,
));

if (!$facebook->getSession()) 
{
    header('Location: ' . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));
}
else
{
    $status = $facebook->api('/me/feed', 'POST', array('message' => 'This post came from my app.'));
    var_dump($status);
}

?>

SDK will handle token him self ;)