Tormy Van Cool
Tormy Van Cool

Reputation: 801

Facebook API 2.2: Graph error when trying to post a link

The goal of the script is to post automatically, on my profile and on my groups, the link to a specific page, once the content is created.

But all what I got when I launch the test script here below, is this returned string:

Graph returned an error: An active access token must be used to query information about the current user.

In fact, the token related to the app was given by FB Developers site. I tried also the combination:

634060413388093|9ed702cc524a1cbb59ca1fb7a17839f1

But I still get the same return

<?php
$path = getcwd();

require_once $path . '/include/Facebook/autoload.php';

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

$linkData = [
  'link' => 'http://www.example.com/page',
  'message' => 'here the new page on my site.',
  ];

try {
    // Returns a `Facebook\FacebookResponse` object
    $response = $fb->post('/me/feed', $linkData, '634060413388093|bnTyPyRtsZSLHoc1B1w772cz3BU');
    } 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'];

?>

Upvotes: 0

Views: 109

Answers (2)

Tormy Van Cool
Tormy Van Cool

Reputation: 801

Ok I found the way in this moment. I tried any example andy help, any suggestion but it didn't work out. Thus, I post it hoping it helps somebody else with my same issue:

The access token, is not app_id|app_secret but it's obtained in another way: To get the correct access otken, one should go to:

https://developers.facebook.com

under "Tools & Support" select "Graph API Explorer" On the upper right corner, there is a drop down menu called "Application". Click and select the created application. Select "Access Token" shown, and copy/paste it in the field aside $linkDAta variable:

$response = $fb->post('/me/feed', $linkData, '{access-token}');

At that point, launching the script as it is, it worked out :)

EDIT: If you need to post in groups, just change the '/me/feed' with '{group_id}/feed'

Upvotes: 0

Sleepy Panda
Sleepy Panda

Reputation: 174

You need to use a user access token to read the feed of a user. 'me' refers to the user that is associated with the user access token. An app access token is not associated with any specific user.

This leads to the second point, which is much more important. You should never reveal your app secret, especially not on Stack Overflow or anywhere public. You should hide that part of your SO Question, or better yet, create a new FB application.

Upvotes: 1

Related Questions