PHP API FACEBOOK - 用户发布到Facebook页面和个人墙

时间:2012-10-10 18:00:36

标签: php facebook facebook-graph-api

我创建了一个提交表单的比赛:

  1. 在Facebook工作人员的墙上写下评论和
  2. 在我的页面墙上写评论
  3. 我对第1步没有任何问题,但第2步不起作用。我的代码如下:

    connect.php

    <?php
    //include the Facebook PHP SDK
    include_once 'facebook.php';
    
    //instantiate the Facebook library with the APP ID and APP SECRET
    $facebook = new Facebook(array(
        'appId' => 'CRYPT FOR THIS FORUM',
        'secret' => 'CRYPT FOR THIS FORUM',
        'cookie' => true
    ));
    
    //Get the FB UID of the currently logged in user
    $user = $facebook->getUser();
    
    //if the user has already allowed the application, you'll be able to get his/her FB UID
    if($user) {
        //start the session if needed
        if( session_id() ) {
    
        } else {
            session_start();
        }
    
        //do stuff when already logged in
    
        //get the user's access token
        $access_token = $facebook->getAccessToken();
        //check permissions list
        $permissions_list = $facebook->api(
            '/me/permissions',
            'GET',
            array(
                'access_token' => $access_token
            )
        );
    
        //check if the permissions we need have been allowed by the user
        //if not then redirect them again to facebook's permissions page
        $permissions_needed = array('publish_stream', 'read_stream');
        foreach($permissions_needed as $perm) {
            if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 ) {
                $login_url_params = array(
                    'scope' => 'publish_stream,read_stream',
                    'fbconnect' =>  1,
                    'display'   =>  "page",
                    'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
                );
                $login_url = $facebook->getLoginUrl($login_url_params);
                header("Location: {$login_url}");
                exit();
            }
        }
    
        //if the user has allowed all the permissions we need,
        //get the information about the pages that he or she managers
        //id pag sposiamo è 494659577226200
        $accounts = $facebook->api(
            '/me/accounts',
            'GET',
            array(
                'access_token' => $access_token
            )
        );
    
        //save the information inside the session
        $_SESSION['access_token'] = $access_token;
        $_SESSION['accounts'] = $accounts['data'];
        //save the first page as the default active page
        //$_SESSION['active'] = $accounts['data'][0];*/
    
        //redirect to manage.php
        header('Location: manage.php');
    } else {
        //if not, let's redirect to the ALLOW page so we can get access
        //Create a login URL using the Facebook library's getLoginUrl() method
        $login_url_params = array(
            'scope' => 'publish_stream,read_stream',
            'fbconnect' =>  1,
            'display'   =>  "page",
            'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
        );
        $login_url = $facebook->getLoginUrl($login_url_params);
    
        //redirect to the login URL on facebook
        header("Location: {$login_url}");
        exit();
    }
    
    ?>
    

    newpost.php

    <?php
    //include the Facebook PHP SDK
    include_once 'facebook.php';
    
    //start the session if necessary
    if( session_id() ) {
    
    } else {
        session_start();
    }
    
    //instantiate the Facebook library with the APP ID and APP SECRET
    $facebook = new Facebook(array(
        'appId' => 'CRYPT',
        'secret' => 'CRYPT',
        'cookie' => true
    ));
    
    //get the info from the form
    $parameters = array(
        'message' => $_POST['message'],
        'picture' => $_POST['picture'],
        'link' => $_POST['link'],
        'name' => $_POST['name'],
        'caption' => $_POST['caption'],
        'description' => $_POST['description']
    );
    
    //add the access token to it
    $parameters['access_token'] = $_SESSION['active']['access_token'];
    
    //build and call our Graph API request
    $newpost = $facebook->api(
        '/494659577226200/feed',
        '/me/feed',
        'POST',
        $parameters
    );
    
    //redirect back to the manage page
    header('Location: manage.php');
    exit();
    

    494659577226200 = FBPAGEID

    问题是'/ 494659577226200 / feed',错误AuthCode 200 ......

1 个答案:

答案 0 :(得分:0)

您需要让您的用户授予您的应用manage_pages权限,以便发布他们代表他们管理的网页。查看他们的权限文档here,请参阅Page Permissions部分。

引自文档: manage_pages

  

使您的应用程序能够检索用户管理的页面和应用程序的access_tokens。可以通过Graph API调用//帐户来查询访问令牌。此权限仅与Graph API兼容,而不与已弃用的REST API兼容。   请参阅此处以生成60天后未过期的长期页面访问令牌。

获得此权限后,您可以使用page access token

制作一个墙贴