I'm trying to make a login system using facebook SDK, because later I'll need some user's data.
On my login page blade I put this code:
session_start();
...
$fb = new Facebook\Facebook([
'app_id' => 'Facebook_id',
'app_secret' => 'Facebook_secret',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl("miwebsite.com/callback", $permissions);
OK. This works good.
Then I put this on my callback controller:
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'Facebook_id',
'app_secret' => 'Facebook_secret',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
echo $_SESSION['facebook_access_token'];
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
And I get this error:
Class 'App\Facebook\Facebook' not found
But if I put the exact code (Copy-pasted) on the view (callback.blade.php) it works O_o
My question is. How can I fix it to work on the controller instead of the view? Because later I'm gonna need to retrieve some data and I need to make this works on controllers... :(
Any clue will help :)
Related
Very new to PHP and downloaded Facebook-PHP-SDK and I am trying to run a simple code I found online but I keep getting an error. I have done lots of research on the problem, looked at similar questions on here and so for but have not been able to find anything that helps. It looks like I still need to authorize the app I created in Facebook Developers, but I cannot figure out how to do that.
<?php
require_once(__DIR__ . '/vendor/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '{}',
'app_secret' => '{}',
'persistent_data_handler' => 'memory',
'default_graph_version' => 'v3.3',
]);
$helper = $fb->getCanvasHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
if (! isset($accessToken)) {
echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
exit;
}
// Logged in
echo '<h3>Signed Request</h3>';
var_dump($helper->getSignedRequest());
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
Error Message
No OAuth data could be obtained from the signed request. User has not authorized your app yet.
You are not supplying the SDK with the required app_id and app_secret so it can't do anything. That's why it gives you an OAUTH error. (fb-docs)
$fb = new Facebook\Facebook([
'app_id' => '{}', // Here
'app_secret' => '{}', // Here
'persistent_data_handler' => 'memory',
'default_graph_version' => 'v3.3',
]);
You need to get that data from your facebook app configuration dashboard. And plug into this object.
example:
$app_id = "1234";
$app_secret = "foobar";
$fb = new Facebook\Facebook([
'app_id' => '{$app_id}',
'app_secret' => '{$app_secret}',
'default_graph_version' => 'v3.2',
]);
Apps are all registered and managed at https://developers.facebook.com/ . Have you checked your settings there?
I'm trying to get user details using fb graph api version 2.10 but couldn't get the email address at all.. I tried searching in google but not got any answer for 2.10.
My code is below:
require 'src/Facebook/autoload.php';
$fb = new \Facebook\Facebook([
'app_id' => 'xxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.9',
// 'default_access_token' => 'qRkpJ79uoLmP6EU649rXAx4bZHs', // optional
]);
$helper = $fb->getRedirectLoginHelper();
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
// $helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=id,email,first_name,last_name,website,work', 'xxxxxxxxxxxxxxxxxxxx');
} 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;
}
$me = $response->getGraphUser();
print_r($me);
This is my controller and it's working fine.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('session');
}
function index(){
$this->load->view("welcome_message");
}
function fblogin(){
$fb = new Facebook\Facebook([
'app_id' => '213234637467346734',
'app_secret' => '7346762476374673647',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email','user_location','user_birthday','publish_actions'];
// For more permissions like user location etc you need to send your application for review
$loginUrl = $helper->getLoginUrl('http://localhost/facebooklogin/welcome/fbcallback', $permissions);
header("location: ".$loginUrl);
}
function fbcallback(){
$fb = new Facebook\Facebook([
'app_id' => '1767865763452676',
'app_secret' => '2454f5b88ea788b407297f39400b796b',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$this->session->set_userdata('state',$_GET['state']);
try {
$accessToken = $helper->getAccessToken();
}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;
}
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=id,name,email,first_name,last_name,birthday,location,gender', $accessToken);
// print_r($response);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'ERROR: Graph ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'ERROR: validation fails ' . $e->getMessage();
exit;
}
// User Information Retrieval begins................................................
$data['me'] = $response->getGraphUser();
$data['accessToken'] =$accessToken;
$this->load->view("home",$data);
}
}
When I refresh the home page its give me this error
Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing from persistent data.
Here I changed the app_id and app_secret for security reason.and i also load the session library in autload file
I have solve the problem.I just add this code in try block where i get the access token
if(!$this->session->userdata('token')){
$accessToken = $helper->getAccessToken();
$this->session->set_userdata('token',$accessToken);
}
else{
$accessToken = $this->session->userdata('token');
}
I have tried to following way
but i got Error "The access token does not belong to application "
i have checked lot of time in my app id and secrt id, app id is correct but This error shown again and again I didn't peridict this Error? and also i have tried following way also
https://graph.facebook.com/v2.2/oauth/access_token?grant_type=fb_exchange_token&client_id=CLIENT_ID
&client_secret=SECRED CODE&fb_exchange_token=EAACEdEose0cBAJRZCZBIaDmW3oOO6SHaOkQLKdgyjp1evGoQ19mYcZCXu5wWLwZABJUbV77tjPjiE2pac2fDEmjM1tZAZB8hflSyERXFWIZB2DtzZAGSgVX6Ukb0ZAZAzd6pohnZBXU0T2aqYwf1umUxsfgHQXBNmM15yhdZBG2Br
PHPsdk 5 and v2.8
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
$client = $fb->getOAuth2Client();
try {
// Returns a long-lived access token
$accessTokenLong = $client->getLongLivedAccessToken($accessToken);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
if (isset($accessTokenLong)) {
// Logged in.
$_SESSION['facebook_access_token'] = (string) $accessTokenLong;
}
Reference url :https://www.sammyk.me/upgrading-the-facebook-php-sdk-from-v4-to-v5
I'm trying to display facebook status updates for a sports team on their website using the facebook graph api, but can't seem to get a valid access token.
Here's my code.
require_once APPPATH.'/third_party/facebook-php-sdk-v4/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123123123123',
'app_secret' => 'fghdfghtyjdfghdghjfghjfghj',
'default_graph_version' => 'v2.5'
]);
$fb->setDefaultAccessToken('123123123123|dfhjfgytdfghdfhgdsfjd');
$helper = $fb->getCanvasHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
if (isset($accessToken)) {
// Logged in.
} else {
echo 'access token not set - THIS IS WHERE I ALWAYS SEEM TO END UP';
}
exit;
Is there anything obvious I'm doing wrong?
This is what I used in a Laravel 4 app to connect. Basically when the user clicks on a 'Log in' button,
1- Create the Facebook instance with app_id, app_secret and default_graph_version.
2- Get a redirectLoginHelper object to get a loginUrl with a code that facebook will provide when you call $helper->getLoginUrl($redirectUrl, (array) $permissionsNeeded)
3- Redirect to that url
4- Create facebook instance and getRedirectLoginHelper again as step 1 and 2
5- Check if you received the 'code' parameter in the url the helped returned
6- Get the access token: $accessToken = $helper->getAccessToken()
In the code I'm providing, I do all this steps in one method. I check if there's a code parameter in the request, if not I request it and redirect. The next time the code runs, when I check if code is there, it continues to the next step
$fb = new Facebook([
'app_id' => 'facebook_app_id',
'app_secret' => 'facebook_app_secret',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
if(Input::get('code') == '') {
$permissions = ['manage_pages, publish_actions'];
$loginUrl = $helper->getLoginUrl('http://yoururl.dev/uri', $permissions);
return Redirect::to($loginUrl);
}
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//Handle error
}
In this case, the Log in button goes to www.domain.app/connect, the URL that the redirectLoginHelper returns is the same, but with the code parameter: www.domain.app/connect?code=ntasd341251