dado.d 0 Newbie Poster

This is my process.php which I use to post data on my page's wall
PHP Code:

include_once("config.php"); 


//HTTP POST request to PAGE_ID/feed with the publish_stream 
$post_url = '/'.$userPageId.'/feed'; 

//after login redirect him and if i can get message submit it to page 
if(isset($_GET["message"])) 
{ 
    $msg_body = array( 
    'message' => $_GET["message"], 
    ); 

    try 
    { 
        $postResult = $facebook->api($post_url, 'post', $msg_body ); 
        //header('Location: ' . $homeurl."?success=true"); 
        $homeurl=$homeurl."?success=true"; 
        echo "<script>window.location='$homeurl'</script>"; 
    } 

    catch (FacebookApiException $e)  
    { 
        echo $e->getMessage(); 
    } 


} 

if($_POST) 
{ 

    //Post variables we received from user 
    $userMessage    = $_POST["message"]; 

    if(strlen($userMessage)<1)  
    { 
        //message is empty 
        $userMessage = 'No message was entered!'; 
    } 


        /* 
        // posts message on page feed 
        $msg_body = array( 
            'message' => $userMessage, 
            'name' => 'Message Posted from Saaraan.com!', 
            'caption' => "Nice stuff", 
            'link' => '', 
            'description' => 'Demo php script posting message on this facebook page.', 
            'picture' => '' 
            'actions' => array( 
                                array( 
                                    'name' => 'Saaraan', 
                                    'link' => '' 
                                ) 
                            ) 
        ); 
        */ 

        //posts message on page statues  
        $msg_body = array( 
        'message' => $userMessage, 
        ); 

    if ($fbuser)  
    { 
        try  
        { 
            $postResult = $facebook->api($post_url, 'post', $msg_body ); 
            $homeurl=$homeurl."?success=true"; 
            echo "<script>window.location='$homeurl'</script>"; 
            //header('Location: ' . $homeurl."?success=true"); 
        }  

        catch (FacebookApiException $e)  
        { 
            echo $e->getMessage(); 
        } 
    } 
    else 
    { 
        $redirect_url=$loginredirect."?message=".$userMessage; 
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$redirect_url,'scope'=>$fbPermissions)); 
        header('Location: ' . $loginUrl); 
        echo "<script>window.location='$loginUrl'</script>"; 
    } 

}  

This is config.php

PHP Code:

include_once("inc/facebook.php"); //include facebook SDK 

######### edit details ########## 
$appId = '604912379583740'; //Facebook App ID 
$appSecret = '<APP SECRET>'; // Facebook App Secret 
//$return_url = 'http://localhost/test/postt/process.php';  //return url (url to script) 
$homeurl = '';  //return to home 
$fbPermissions = 'publish_stream,manage_pages,publish_actions';  //Required facebook permissions 
$loginredirect = '';  //return to home 
//Post variables we received from user 
$userPageId = "267294363430972";//$_POST["userpages"]; 
################################## 

//Call Facebook API 
$facebook = new Facebook(array( 
  'appId'  => $appId, 
  'secret' => $appSecret 
)); 

$fbuser = $facebook->getUser();  

in index.php I'm just using simple form with textarea and submit button to submit to process.php
It just says (#200) The user hasn't authorized the application to perform this action. I'm not sure where did I make a mistake. I tried with 2 different accounts and they all allowed my app to preform those actions

Dani AI

Generated

Quick diagnosis and the most likely cause

The Graph error "(#200) The user hasn't authorized the application to perform this action" almost always means the access token used for the post does not carry the right Page-level privileges. When an app tries to POST to a Page feed with a normal user token (or a token missing the Page scopes), Facebook rejects the request. The usual fix is not to retry with the same user token, but to use the Page access token you obtain after a Page admin grants the proper permissions. (stackoverflow.com)

What to do (practical flow to follow)

  1. Make sure the login flow requests the current Page publishing scopes (modern names vs older names vary over API versions).
  2. After an admin authorizes the app, call the accounts edge (for example GET /me/accounts) or the page fields endpoint to retrieve that Page’s access_token.
  3. Use that Page access token when you POST to /{page-id}/feed. Test the same call in the Graph API Explorer first to confirm it works before wiring it into your app. (stackoverflow.com)

Permissions, review and deprecated scopes (important cautions)

Legacy scopes you may see in old examples (publish_stream, publish_actions) have been changed or removed in newer Graph API versions. To let non-admin users publish via your app you will now typically need Page-level permissions and App Review; admin accounts can often test without full review. Also be aware of token expiry and business/2FA requirements that can block publishing. (stackoverflow.com)

Quick troubleshooting checklist

  • Inspect the token you’re using with the Access Token Debugger / Graph API Explorer and confirm it is a Page token and lists the expected scopes.
  • If you only have a short-lived user token, exchange it for a long-lived token and then fetch the Page token via /me/accounts.
  • Confirm your app is Live and that the requested permissions are approved if other people (non-admins) must post. If it still fails, capture the full error JSON and fbtrace_id and re-check the token scopes. (stackoverflow.com)

Notes for : the server-side flow in your description will succeed once you explicitly fetch and use the Page access token (and ensure the app/permissions are correct).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.