skip to Main Content

Hi guys I´m new in the PHP SDK API from Facebook, now I have some troubles with a simple GET query here it´s my controller’s code:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Facebook extends CI_Controller
{
    public $app_id = 'XXXXXXXX';
    public $app_secret = 'XXXXXXXXXX';

    public function __construct()
    {
        parent::__construct();
        $this->load->library('FacebookSDK');
    }

    public function index()
    {
        $fb = new FacebookFacebook(array(
            'app_id' => $this->app_id,
            'app_secret' => $this->app_secret,
            'default_graph_version' => 'v2.10',
        ));

        $helper = $fb->getRedirectLoginHelper();
        $accessToken = $helper->getAccessToken();

        try {
            $request = $fb->get('/me/events', $accessToken);
            $eventos = $request->getGraphObject()->asArray();
            $datos['eventos'] = $eventos;
            $this->load->view(home, $datos);
            /* handle the result */
        } catch (FacebookExceptionsFacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: '.$e->getMessage();
        } catch (FacebookExceptionsFacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: '.$e->getMessage();
        }
    }
}

The error throws me “Facebook SDK returned an error: You must provide an access token.”
PD: I´m working with Codeigniter

2

Answers


  1. You have not provide value for $app_id and $app_secret.

    public $app_id = 'XXXXXXXX';
    public $app_secret = 'XXXXXXXXXX';
    

    app_id is your app id on Facebook and app_secret is a token to access your app from facebook API.

    Create an app on Facebook Developer, then you will get app_id and app_secret. After that fill that variable with your own app_id and app_secret like this:

    public $app_id = '123124123123';
    public $app_secret = 'd45f11ba49e93122b94ea814c1d7611c';
    
    Login or Signup to reply.
  2. If the user has not previously logged in your app, Facebook SDK won’t generate any access token.

    In that case:

    $accessToken = $helper->getAccessToken();
    

    The above code will fail and return an error and that is the reason why this code is under try-catch block in official Facebook PHP SDK documentation

    So you should change your code to generate the access token first (it will be generated after user will authenticate your app) and then use that access token to fetch data from the graph API endpoints.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search