How do I authorize a app in Facebook-PHP-SDK - php

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?

Related

Exception 1 Facebook PHP SDK

I have a code that if a do click on login with Facebook button works ok and prints me the correct data that I expect... Now the thing is that if I reload throws me the exception "Exception 1: This authorization code has been used."
ThereĀ“s my callback.php code
$fb = new Facebook\Facebook(array(
'app_id' => $this->app_id,
'app_secret' => $this->app_secret,
'cookie' => true,
'status' => true,
'default_graph_version' => 'v2.10',
'http_client_handler' => 'curl', // can be changed to stream or guzzle
'persistent_data_handler' => 'session', // make sure session has started
));
$helper = $fb->getRedirectLoginHelper();
if (isset($_GET['state'])) {
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
}
try {
// get access token
$accessToken = $helper->getAccessToken();
print_r($accessToken);
// save access token to persistent data store
$helper->getPersistentDataHandler()->set('access_token', $accessToken);
} catch (Exception $e) {
// error occured
echo 'Exception 1: ' . $e->getMessage() . '
';
}
// get stored access token
$accessToken = $helper->getPersistentDataHandler()->get('access_token');
if ($accessToken && !$accessToken->isExpired()) {
// set default access_token so we can use it in any requests
$fb->setDefaultAccessToken($accessToken);
}
Any idea?

Graph returned an error requires extended permission business_management

My app has ads_management, business_management and standard level permissions so When I try to login in from my Facebook account then it retrieves information successfully. But when my customer login with fb then it shows error
Graph returned an error: (#200) Requires extended permission: business_management
I am making following API call v2.8
$fb = new \Facebook\Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => $graph_api_version,
]);
$response = $fb->get('/me/businesses', $accessToken);
What can be the reasons and possible solutions?
Try this
$fb = new \Facebook\Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => $graph_api_version,
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = [
'email',
'user_friends',
'manage_pages',
'business_management',
'ads_management'
];
$fb_url = $helper->getLoginUrl(
<callbackUrl>,
$permissions
);
Redirect use to this url. Facebook will redirect the user again back to the callback url after taking permissions from the user.
Once customer comes back to portal via this url, you will get the access token having permissions of the extended permission which you exchange for long-lived access token by short-lived access token and query the requried graph-api
Callback Script
$fb = new \Facebook\Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => $graph_api_version,
]);
$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;
}
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchange short-lived access token with long-lived access token
if (!$accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
}
Store access token and redirect user where you query this
$response = $fb->get('/me/businesses', $accessToken);

How to get Long live access token for Graph Api For PHP facebook SDK?

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

Getting an access token from the Facebook Graph API

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

laravel facebook class not load only on controllers

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 :)

Categories