Facebook PHP SDK - Getting error when posting to f

2019-05-19 03:05发布

I don't know why but I get the following error when I'm trying to post to a facebook page that I've created:

Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message 'Unsupported post request.

Here's the code that I currently have:

<?php
session_start();

require 'vendor/autoload.php';


use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;


FacebookSession::setDefaultApplication('my-app-id', 'my-app-secret');

$session = new FacebookSession('my-access-token');

if($session){

    $request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms');
    $pageList = $request->execute()->getGraphObject()->asArray();
    $token = $pageList['data'][0]->access_token;

    $request2 = new FacebookRequest(
        $session,
        'POST',
        '/my-page-id/feed',
        array (
          'access_token' => $token,
          'name' => 'MySQL: Why My?',
          'caption' => 'MySQL: Why My?',
          'link' => 'http://www.vertabelo.com/blog/mysql-history',
          'message' => 'MySQL: Why My?',
        )
    );

    $response2 = $request2->execute();
    print_r($response2);

}

An error is returned but the post is actually posted in the facebook page when I check. Am I doing something wrong here?

Another alternative that I tried is to get the page access token from another page load and then set it on the new FacebookSession call instead of the access token of my profile. Then I omit the access_token in the request for creating a new post. It also returns an error but the post is also created on the page. Any ideas?

2条回答
倾城 Initia
2楼-- · 2019-05-19 03:20

I also seen similar kind of errors, when i was writing code for to develop Facebook APP.

I used same idea, made another call to get fresh access token. Then made Graph API call with newly created token. This worked for me, and also my app was still in development mode.

I seen code and think that you are passing parameters in Graph API URI these are not needed to make call. Don't use like this

$request = new FacebookRequest($session, 'GET','/me/accounts?fields=name,access_token,perms');

Instead try this $request = new FacebookRequest($session, 'GET', /me/accounts');

Happy Coding! Atul Jindal

查看更多
We Are One
3楼-- · 2019-05-19 03:35

This is specifically related to POSTing the link field to the /{page-id}/feed endpoint.

This error is returned when your app is in development mode. If you make the app live, it should work.

Pretty sure this is not the intended behavior, so I submitted a bug report.

查看更多
登录 后发表回答