-1

I am trying to implement auto posting through cron to a facebook site that I created. The hard part about this is: no account is logged in during the process.

I checked a lot of sites for my problem and somehow it still doesn't work... Does anybody see any problem in my code QQ HELP

$username = "xx";
$password = "xx";

// access to facebook home page (to get the cookies)
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, "http://www.facebook.com" );
curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_ENCODING, "" );
curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );
curl_setopt ( $curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
$curlData = curl_exec ( $curl );


// do get some parameters for login to facebook
$charsetTest = substr ( $curlData, strpos ( $curlData, "name=\"charset_test\"" ) );
$charsetTest = substr ( $charsetTest, strpos ( $charsetTest, "value=" ) + 7 );
$charsetTest = substr ( $charsetTest, 0, strpos ( $charsetTest, "\"" ) );

$locale = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );
$locale = substr ( $locale, strpos ( $locale, "value=" ) + 7 );
$locale = substr ( $locale, 0, strpos ( $locale, "\"" ) );

$lsd = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );
$lsd = substr ( $lsd, strpos ( $lsd, "value=" ) + 7 );
$lsd = substr ( $lsd, 0, strpos ( $lsd, "\"" ) );

// do login to facebook
curl_setopt ( $curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1" );
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, 
    "charset_test=" . $charsetTest . 
    "&locale=" . $locale . 
    "&non_com_login=&email=" . $username . 
    "&pass=" . $password . 
    "&charset_test=" . $charsetTest . 
    "&lsd=" . $lsd );
curl_setopt ( $curl, CURLOPT_COOKIEFILE, getcwd () . '/cookies_facebook.cookie' );
$curlData = curl_exec ( $curl );//this actually is the code of my facebook profile page so it works


//Page access_token
$access_token = "xx";   
$params=array(
      'access_token'=>$access_token, 
      'message'=> "Testing newwjjj7wwwg", 
      'name'=>'TEST6678888');

//Postng to facebook page
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/".variable_get('webconnect_facebook_pageId')."/feed");
$result = curl_exec($curl);

curl_close ($curl);

When I try to make a post: { "error": { "message": "Error validating access token: This may be because the user logged out or may be due to a system error.", "type": "OAuthException", "code": 190, "error_subcode": 467 } }

When I try to use the user access_token: { "error": { "message": "(#200) The user hasn't authorized the application to perform this action", "type": "OAuthException", "code": 200 } }

The thing is I did authorize it... Any ideas ??

  • How exactely did you authorize the user? A normal user access token doesn't do the trick. You need the extended permission `publish_stream`. https://developers.facebook.com/docs/reference/login/extended-permissions/ – thpl Jan 31 '14 at 12:37

2 Answers2

0

Can your code post in a browser session while the user is logged in to Facebook and the access token is valid? You can create a new test user to make sure that the access token is valid and that the right permission is being granted. If the app works in that scenario, leave the user logged in and try with your original test case (using the access token from the first test). That should tell you whether there's a problem with your original code or if it's a session/data (access code, permissions) problem.

kaared
  • 63
  • 9
0

When I try to make a post: { "error": { "message": "Error validating access token: This may be because the user logged out or may be due to a system error.", "type": "OAuthException", "code": 190, "error_subcode": 467 } }

That is because the lifetime of the token got expired. You need to extend the lifetime of the token. See here.

When I try to use the user access_token: { "error": { "message": "(#200) The user hasn't authorized the application to perform this action", "type": "OAuthException", "code": 200 } }

For this you need to check the tickbox near status_update of your permissions. See here.

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126