Facebook SDK PHP插件内

时间:2015-11-01 10:34:26

标签: php facebook facebook-graph-api wordpress

我收到此错误:

Graph returned an error: (#200) The user hasn't authorized the application to perform this actionPosted with id: 136766596799963

我的WP插件具有以下结构:

myplugin/
    |-css/
    |-images/
    |-includes/
        data-processing.php
    |-js/
    |-src/
        |-facebook-sdk-v5/
            autoload.php + other fb sdk files and directories
    myplugin.php

在myplugin.php中我有这个:

<?php
error_reporting(E_ALL);
/*
Plugin Name: MY PLUGIN
*/

/************************
* global variables
************************/

$plugin_prefix  = 'MYPLUGIN_';
$plugin_name    = 'MYPLUGIN';

/************************
* includes
************************/

// Facebook SDK
require_once dirname(__FILE__) . ('/src/facebook-sdk-v5/autoload.php');

include_once dirname(__FILE__) . ('/includes/data-processing.php');

在我的data-processing.php文件中,我有:

<?php
/************************
* main plugin code 
************************/

use Facebook\FacebookRequest;


class Myplg {

    public $options;

    public function __construct()
    {
        $this->options = get_option('cstm_myplugin_plugin_options');
        $this->register_settings_and_fields();
    }

    public static function add_menu_page()
    {
        $page_title = 'MY PLUGIN';
        $menu_title = 'MY PLUGIN';
        $capability = 'administrator';
        $menu_slug  = __FILE__;
        $function   = array('Myplugin', 'display_options_page');
        $icon_url   = plugins_url( '../images/ico_myplugin.png' , __FILE__ );
        $position   = 3;
        add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    }

    public function display_options_page()
    {

        $plugin_options = get_option('cstm_myplg_plugin_options');

        $fb = new Facebook\Facebook([
            'app_id' => '918761491987952';
            'app_secret' => 'rtaas683asdeefb3d78cas3cb849888dfsdf9b3';
            'default_graph_version' => 'v2.2',
        ]);

        $access_token = 'CA9fdidTJJ9cBAOCyWA23TqAna9eGZCsZBZBvCYhgG2MZB5Bx15d8uhNaILwUJ6HFMSgua7Q4MRZB3ZAHi3o84UDX3qUrLJ5Ie4WLaB5YhyRgTrZCIu1LGLOL3qfKoANNykPhuM24g82bWBN52lbpdoedya1POmBTyZBZB2FV908jlxxFp5aFU9za1IJ4juGWAKpM4e37MusVI0ZA3uvDWEfsRd';

        $fb->setDefaultAccessToken($access_token);

        //GET USER NAME
        try {
          $response = $fb->get('/me');
          $userNode = $response->getGraphUser();
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
          // When Graph returns an error
          echo 'Graph returned an error: ' . $e->getMessage();
          exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
          // When validation fails or other local issues
          echo 'Facebook SDK returned an error: ' . $e->getMessage();
          exit;
        }

        echo 'Logged in as ' . $userNode->getName(); // works ;)

        //POST A TEST MESSAGE ON A PAGE WALL
        $linkData = [
            'link'    => 'http://www.example.com',
            'message' => 'Test message',
        ];

        try {
          // Returns a `Facebook\FacebookResponse` object
          $response = $fb->post('/me/feed', $linkData, $access_token);
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
          echo 'Graph returned an error: ' . $e->getMessage();
          //exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
          echo 'Facebook SDK returned an error: ' . $e->getMessage();
          //exit;
        }

        $graphNode = $response->getGraphNode();

        echo 'Posted with id: ' . $graphNode['id']; // doesn't work ;(


} // END OF CLASS

add_action('admin_menu', function() {
    Myplg::add_menu_page();
});

add_action('admin_init', function() {
    new Myplg();
});

因此,结果是我可以看到用户的名字,但我无法在Facebook页面墙上发布消息。

我得到了这个:

Logged in as john doeGraph returned an error: (#200) The user hasn't authorized the application to perform this actionPosted with id: 136766596799963

知道出了什么问题吗?

0 个答案:

没有答案