samedi 18 avril 2015

Facebook connect and post php

i've got this php code that uses macbook sdk4 to publish post in user's wall, but it doesn't work. it gives me this error when i try to login:



Fatal error: Uncaught exception 'Facebook\FacebookClientException' with message 'Duplicate status message' in /membri/bestparty/APP/facebook/src/Facebook/FacebookRequestException.php:122 Stack trace: #0 /membri/bestparty/APP/facebook/src/Facebook/FacebookRequest.php(268): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 500) #1 /membri/bestparty/APP/facebook/fbconfig.php(43): Facebook\FacebookRequest->execute() #2 {main} thrown in /membri/bestparty/APP/facebook/src/Facebook/FacebookRequestException.php on line 122



this is the code



<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
require 'functions.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','xxx' );
$required_scope = 'public_profile, publish_actions, email, manage_pages'; //Permissions required
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://ift.tt/1yVTdGL' );
try {
$session = $helper->getSessionFromRedirect();

} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email');

// TRY PUBLISH
$postrequest = new FacebookRequest( $session, 'POST', '/me/feed', array('message' => 'Ho appena scaricato DONUT, provala anche tu!'));
$postresponse = $postrequest->execute();
$posting = $postresponse->getGraphObject();
echo $posting->getProperty('id');


/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
checkuser($fbid,$fbfullname,$femail);
/* ---- header location after session ----*/


$cookie_name = 'FBID';
$cookie_value = $fbid;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), '/');
$cookie_name2 = 'FULLNAME';
$cookie_value2 = $fbfullname;
setcookie($cookie_name2, $cookie_value2, time() + (86400 * 30), '/');


/*echo" <script type=\"text/javascript\">
function goBack() {
javascript: history.go(-1);
}
window.onload=goBack;
</script>";*/ /*PROVA PER LOCALE TORNARE INDIETRO HISTORY*/

header("Location: ../loginpage.php");

} else {

$cookie_name = 'FBID';
$cookie_value = $fbid;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), '/');
$loginUrl = $helper->getLoginUrl( array( 'scope' => $required_scope ) );


header("Location: ".$loginUrl);
}
?>


if i delete the user's post it works fine:



// TRY PUBLISH
$postrequest = new FacebookRequest( $session, 'POST', '/me/feed', array('message' => 'Ho appena scaricato DONUT, provala anche tu!'));
$postresponse = $postrequest->execute();
$posting = $postresponse->getGraphObject();
echo $posting->getProperty('id');


so i think that the problem is on it, but where?


and also, how can i do to post on user wall from another page without login request another time?


and the last question is, why cookies aren't stored when i close the browser?


Aucun commentaire:

Enregistrer un commentaire