My app has been approved for permissions, but cannot use the approved permissions when logging in - facebook

I am making a website with the function of linking to facebook and creating posts on fanpage. I have created facebook app and the permissions have been approved enter image description here. All works fine when I login with the admin account of the app but when I login with another user account there is no dialog asking for permission. This resulted in the app not getting the token with the permissions I needed to post.
Hope you can help me.
This is connect function
public function connect($user)
{
$urlAuth = Socialite::driver('facebook')
->scopes([
'pages_manage_metadata',
'instagram_basic', 'instagram_content_publish',
'pages_manage_posts', 'pages_read_engagement',
'pages_show_list'
])
->with(['state' => $user->id])
->stateless()->redirect()
->getTargetUrl();
return $urlAuth;
}
This is callback function
public function callback()
{
$user = Socialite::driver('facebook')->stateless()->user();
$token = $user->token;
if($token){
$facebook = new FacebookPublisher($token);
$account = $facebook->getAccount()->data ?? [];
$userID = request()->state;
if(count($account) == 1){
$account = $account[0];
$userInit = User::findOrFail($userID);
$this->savePageInfo($token, $userInit, $account);
$url = config("api.client.connect_account_url");
}else{
$this->saveAccessToken($token, $pageToken = "", $userID);
$url = config("api.client.connect_page_url");
}
return redirect($url);
}
return $this->error(trans('Connect fail!'), [], 404);
}
Thank you,

Related

Facebook login with codeigniter working in localhost but not working on server(live),?

please help me, I am in little trouble. I am familiar with codeigniter.
but I struggling with Login in With Facebook button. I included all files like facebook.php and base_facebook. Its working in Localhost but not working on server. please help me to fix this issue. when I click login button it's responding blank. I given my controller coding below. Thanks in advance.
public function check_fb()
{
$this->load->model('users/users_model');
$appId = '************'; //Facebook App ID
$appSecret = '************'; // Facebook App Secret
$homeurl = 'Site URL'; //return to home
$fbPermissions = 'email'; //Required facebook permissions
parse_str($_SERVER['QUERY_STRING'],$_REQUEST);
$this->load->library('Facebook', array("appId"=>"******", "secret"=>"*******"));
$fbuser = $this->facebook->getUser();
// Here it self I did't get any response.
if(!$fbuser){
$fbuser = null;
$loginUrl = $this->facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
//echo $loginUrl; exit;
redirect($loginUrl);
}
else
{
$user_profile = $this->facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
$user = new Users();
//echo "<pre>"; print_r($user_profile); exit;
if(!empty($user_profile)){
if(!empty($user_profile['id']))
{
$check=$this->users_model->check_login_facebook_id($user_profile['id']);
if($check)
{
echo 'already Exist';
}
else
{
$result = $this->users_model->update_login_facebook_id($user_profile['id']);
redirect($this->config->item('base_url').'users/dashboard');
}
}
}
}
}
Above code is working pretty cool..! I made mistake in library folder. I renamed base_facebook.php as Base_facebook.php, its case sensitive. if anyone face like this problem, just check twice your library files name. Facebook.php ,base_facebook.php, Fb_ca_chain_bundle security certificate.

Retrieving Facebook friends list

I am not able to get the Facebook friends list using the API. It was working fine earlier but not anymore. Here is the code I have used. Can someone help to validate the code. Thanks in advance.
<?php
require_once('PHPMailerAutoload.php');
require_once('PHPMailerConfig.php');
require_once("facebook/facebook.php");
$config = array();
$config['appId'] = '***************';
$config['secret'] = '*******************************';
$facebook = new Facebook($config);
$uid = $facebook->getUser();
if(!empty($_GET['error'])) {
echo "User cancelled the authentication with facebook";
echo "<script>
window.opener.location.href='merror.php';
window.close();
</script>";
exit;
}
if($uid) {
$url = $uid.'?fields=id,work,friends,email,picture.width(450).height(450)';
$user_profile = $facebook->api($url);
$friends = $facebook->api('/me/friends');
$d = '';
foreach($friends['data']as $val) {
$d .= $val['id'].',';
}
}
?>
Please read the docs before posting a question which has been answered here hundreds of times.
See
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read
A user access token with user_friends permission is required to view the current person's friends.
This will only return any friends who have used (via Facebook Login) the app making the request.
If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.

Laravel, Facebook API: retrieving friends of friends

In a previous thread we were strugling with the Facebook login using a Laravel app. Now, as the user logs into our app using Facebook, we are trying to get his friend's list in order to provide some qualified suggestions. So, here's what we are trying:
Route::get('login/fb/callback', function() {
// A FacebookResponse is returned from an executed FacebookRequest
$helper = new FacebookRedirectLoginHelper('http://yoururl/login/fb/callback');
$session = $helper->getSessionFromRedirect();
$request = new FacebookRequest($session, 'GET', '/me/friends');
try {
$response = $request->execute();
$me = $response->getGraphObject();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
print_r($me);//->getProperty("id"));
$user = new Giftee;
$user->name = $me->getProperty('first_name') . ' ' . $me->getProperty('last_name');
$user->email = $me->getProperty('email') . "#facebook.com.br";
$user->photo = 'https://graph.facebook.com/' . $me->getProperty('id') . '/picture?type=large';
$user->save();
});
As a result, we get:
object(Facebook\GraphObject)#146 (1) { ["backingData":protected]=> array(0) { } }
By reading the Facebook API docs, they pretty much say user_friends will only return friends of friends that already use your app and We could not see any login permission that would just return friends of friends (not only the ones using our app). In another words, is what we want really impossible?
You're not able to retrieve friends of friends. With Graph API v2.0, you even only get those friends of your app's users which also use your app.
And, all friends_* permissions have been removed. So I don't see a chance for you to implement what you want to achieve.

Symfony 2.1 Autologin after facebook login

I'm creating an intranet, therefore all users have account automatically created for them. They then go to a url to create their password. All that works great. I'm now trying to allow them to "attach" their Facebook account to their user account. This is where my troubles have started...
I've successfully gotten the user to authenticate properly after a facebook login with this
method in SecurityController
public function loginCheckFacebookAction()
{
$request = $this->getRequest();
$session = $request->getSession();
$response = new Response();
$sessionUser = $this->getUser();
$facebook = $this->get('facebook');
$fbUser = $facebook->getUser(); // facebook id
if ($fbUser) {
try {
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository("IntranetBundle:User");
$user = $repo->findUserByFBID($fbUser);
if($user){
$targetPath = $session->get("_target_path");
$firewall = "secured_area";
$roles = $user->getRoles();
$rolesArray = array();
foreach($roles as $role):
$rolesArray[] = $role->getRole();
endforeach;
$token = new UsernamePasswordToken($user, $user->getPassword(), $firewall, $rolesArray);
$event = new InteractiveLoginEvent($this->getRequest(), $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
$this->get('security.context')->setToken($token);
/*
If I uncomment out either of the following redirects
the redirect happens but the user is not authenticated properly
If I comment them out I'm just rendering my error template. The user
however, is authenticated properly.
*/
if(isset($targetPath)){
//return $this->redirect($targetPath);
} else {
//return $this->redirect($this->generateUrl("ssla_intranet_homepage"));
}
}
} catch (FacebookApiException $e) {
$user = null;
}
// I just render this template to be able to view debug bar.
//User is authenticated properly.
$pageParameters = $this->getPageParameters();
$pageParameters["page"] = "error";
$pageParameters["error"] = array();
$pageParameters["error"]["message"] = "Something went wrong with authentication";
return $this->render('IntranetBundle:Error:index.html.twig', $pageParameters);
}
Do I need to register a listener for all interactive_login events that then handles all post-login redirection rather than trying to redirect within the same method?
First project using Symfony, and this is killing me. Any thoughts?
By the way, I've looked into the FOSFacebookBundle, but wanted to be able to authenticate with a number of different methods, including doctrine, gmail, facebook, and twitter.

Facebook API how to get User Pages

I am using the following code to grab info for the Facebook user.
Is there a way to get the user Pages (if the user has one or more Facebook Pages) and the IDs of these pages?
$user = $_POST['uid']; //the returned user ID from Facebook
$key = $_POST['usid']; //the returned session ID from Facebook
$accesstoken = $_POST['accesstoken']; //the returned accesstoken from Facebook
$image = "http://graph.facebook.com/" .$user ."/picture?type=large"; //Facebook Profile Picture
$name = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->name; //Name of the user account on Facebook
$link = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->link; //Link of the user profile on Facebook
$gender = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->gender; //Gender of the user on Facebook
$locale = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->locale; //Location of the user on Facebook
$url = "https://graph.facebook.com/". $user ."/feed?limit=1&access_token=".$accesstoken; //We get the latest status update on the Facebook Wall
$json = file_get_contents($url);
$jsonData = json_decode($json);
foreach($jsonData->data as $val) {
if($val->from->id == $user) {
$statusmessage = $val->message; //Finally we got the latest status update if there is one on The Facebook Wall
break;
}
}
If you asking how to get additional user profiles, of a single user, based on their Facebook Id: the answer is you can't. Facebook mandates one profile per user. And while in real life a user can have multiple profiles, there is no reliable means of extracting that data from the graph.
However, if you're asking how to get the ids of pages the user admins then use:
https://graph.facebook.com/USER_ID/accounts
this will require a valid access token for the given user.