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
You have not provide value for
$app_id
and$app_secret
.app_id
is your app id on Facebook andapp_secret
is a token to access your app from facebook API.Create an app on Facebook Developer, then you will get
app_id
andapp_secret
. After that fill that variable with your ownapp_id
andapp_secret
like this:If the user has not previously logged in your app, Facebook SDK won’t generate any access token.
In that case:
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.