简体   繁体   English

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

[英]PHP API FACEBOOK - Users post to facebook page and personal wall

I created a contest where a submitted form will: 我创建了一个竞赛,提交的表单将:

  1. write a comment on the wall of Facebook staff and 在Facebook员工的墙上写下评论,
  2. write a comment on the wall of my page 在我的页面墙上写评论

I had no problems with step 1, but step 2 does not work. 我对步骤1没问题,但是步骤2不起作用。 My code is as follows: 我的代码如下:

connect.php 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 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 = FBPAGEID

PROBLEM IS '/494659577226200/feed', and error AuthCode 200... 问题是'/ 494659577226200 / feed',错误AuthCode 200 ...

You need to ask your user's to give your app manage_pages permission to post to their pages they manage on behalf of them. 您需要要求用户授予应用程序manage_pages权限,才能发布到他们代表他们管理的页面上。 Check out their permissions doc here , See Page Permissions section. 在此处查看他们的权限doc,请参阅Page Permissions部分。

Quoted from docs: manage_pages 引用自文档: manage_pages

Enables your application to retrieve access_tokens for Pages and Applications that the user administrates. 使您的应用程序能够检索用户管理的页面和应用程序的access_tokens。 The access tokens can be queried by calling //accounts via the Graph API. 可以通过Graph API调用// accounts来查询访问令牌。 This permission is only compatible with the Graph API, not the deprecated REST API. 该权限仅与Graph API兼容,不兼容不建议使用的REST API。 See here for generating long-lived Page access tokens that do not expire after 60 days. 请参阅此处,以生成60天后不会过期的长寿命页面访问令牌。

Once you get this permission, you can then make a wall post using page access token 获得此权限后,您就可以使用页面访问令牌发表留言了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM