Facebook app permissions when fanpage is liked - facebook

I had wanted to check if people like my fanpage so I had to create app on Facebook. I connected themselves on Facebook too. Below my code:
require_once('facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => '574707409300181',
'secret' => 'xxxxxx',
'cookie' => true));
$user = $facebook->getUser();
if($user) {
try {
$our_page_id = '263807853767281'; // This should be string
$user_is_fan = false;
$likes = $facebook->api( '/me/likes?fields=id' );
foreach( $likes['data'] as $page ) {
if( $page['id'] === $our_page_id ) {
$user_is_fan = true;
echo 'YOU LIKE FANPAGE!';
}
}
}
catch (FacebookApiException $e) {
$user = null;
}
}
My question - when user press "like" button on Facebook is possible to show window with the permission's accepting of my app? I'm asking because i don't know how promote my app... Maybe is there better solution? In short words - when user likes my fanpage he have to accept permissions. :)
Thank you for reply!

Related

Facebook OAuth Issue with CakePHP 2.9

Hi Guys first of all let me tell you guys that I am a newbie with CakePHP and MVC also but one of my old client requested me some favor which I took as a challenge. So If anyone can help me I will appreciate a lot.
I have a CakePHP 2.9 application where OAuth is not working properly following is the code for app/Controller/UsersController.php
public function facebookLogin(){
$this->layout = false;
App::import('Vendor', 'Facebook/facebook');
/*$facebook = new Facebook(array(
'appId' => '1867587306812489', // Facebook App ID
'secret' => '9fb41a07d3da3406dbab5861c3498764', // Facebook App Secret
'cookie' => false,
)
); */
$facebook = new Facebook(array(
'appId' => '1887387108164606', // Facebook App ID
'secret' => '8d733aa6a4501ce27fde9626429baab9', // Facebook App Secret
'cookie' => false,
)
);
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=first_name,last_name,email');
$usersArr = $this->User->find('first',array('conditions'=>array('User.email'=>$user_profile['email'],'User.social_id'=>$user_profile['id'],'User.role_id'=>0)));
$user_count = $this->User->find('first',array('conditions'=>array(),'order'=>'User.id DESC','limit'=>1));
if(empty($usersArr)){
$saveUserArr['User']['username'] = trim($user_profile['first_name']).' '.trim($user_profile['last_name']);
$saveUserArr['User']['firstname'] = $user_profile['first_name'];
$saveUserArr['User']['lastname'] = $user_profile['last_name'];
/* $user_n = $saveUserArr['User']['first_name'].'.'.$saveUserArr['User']['last_name'];
$user_cnt = $user_count['User']['id'] + 1;
$user_n = str_replace(' ','_',$user_n).'.'.$user_cnt;
$saveUserArr['User']['user_unique_id'] = $user_n; */
$saveUserArr['User']['email'] = $user_profile['email'];
$saveUserArr['User']['password'] = '123456';
$saveUserArr['User']['social_id'] = $user_profile['id'];
$saveUserArr['User']['login_from'] = 'facebook';
$saveUserArr['User']['role_id'] = 0;
$this->User->save($saveUserArr['User'],false);
}else{
$this->User->id = $usersArr['User']['id'];
$this->User->saveField('social_id', $user_profile['id']);
$this->User->saveField('login_from', 'facebook');
}
$this->request->data['User']['email'] = $user_profile['email'];
$this->request->data['User']['login_from'] = 'facebook';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
return $this->redirect($loginUrl);
}
}
private function deleteFacebookSession(){
Configure::write('debug', 0);
if(!$this->Session->check('Auth.User')){
foreach( $this->Session->read() as $key => $value ) {
if( strpos( $key, 'fb_' ) === 0 ) {
$this->Session->delete($key);
}
}
}
}
It throws following error:
The oauth script url is not properly configured in your fb app.
You may go to your fb app settings and look for APP DOMAIN.
Include your domain there:
If you are developing in your localhost, you may want to create a server.local rec in your hosts file so you can access your localserver as server.local.
Also, you can create test versions of the main fb app, and allows you to have a copy of your fb app that works in other environments, it is called "create test app"

fb login error in php

I am creating fb login with the php ,
the problem is that the email of user is not returning from facebook,
FBID and USER NAME is returning.
The same code i have done for another project is working but for the new one is not working
the code is like this
require 'src/facebook.php'; // Include facebook SDK file
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXX', // Facebook App ID
'secret' => 'XXXXXXXXXXXXXXXXX', // Facebook App Secret
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['USERNAME'] = $fbuname;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
header("location:fblogin.php");
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
header("location: " . $loginUrl);
}
$user_profile = $facebook->api('/me?fields=name,email');
It´s called "Declarative Fields", see changelog. Btw, you can´t get the username anymore.

Facebook GetUser() returns 0 without any changes of my code

I have user login implemented on my website. This was working good until now. When i try to login with Facebook account i always get 0 from GetUser(). I tryed with different web browser, updated PHP SDK to latest version, but still not working. Any clue ?
$facebook = new Facebook(array(
'appId' => 'my APP ID',
'secret' => 'my Secret',
'cookie' => false, // enable optional cookie support
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$likes = $facebook->api("/me/likes/ID HIDDEN");
if (!isset($_SESSION['prijavljen'])) {
$_SESSION['prijavljen'] = True;
}
if (!empty($likes['data'])) $fblike = true; else $fblike = false;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
//$logoutUrl = $facebook->getLogoutUrl();
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'my URL'));
} else {
$params = array('scope' => 'email,user_birthday,user_education_history,user_hometown,user_likes', 'redirect_uri' => curPageURL());
$loginUrl = $facebook->getLoginUrl($params);
}
Was the PHP version on your server recently updated? You can try this fix and see if getUser() function starts working for you again.

Facebook login to site bugs

When users come to my site they have the option to log in through facebook. If they are logged in to facebook, and hit the fb login button, it will show all the appropriate logged in dialog. However, if you log out of facebook (either through my site or through facebook itself) and then try to log in through my site again, it will bring you to facebook to sign in, then bring you back to my site. The url changes from x.net/index.php to x.net/index.php?state=... bunch of letters / numbers, but then doesn't return logged in dialog. Hit the button once again after that, and the page refreshes with a different (but similar) url, and returns all the logged in dialog.
Any ideas on how to fix this?
Here is the code I use to connect to facebook and get the login and logout urls.
//facebook application
$fbconfig['appid' ] = "myAppId";
$fbconfig['secret'] = "myAppSecret";
$fbconfig['baseurl'] = "http://myNeatSite.net/index.php";
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream',
'redirect_uri' => $fbconfig['baseurl']
)
);
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
As far as I see, you only process the case $user is not null (which means the user is already logged in), but ignore the other case.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}

Facebook photo tagging app not tagging people

I have a facebook photo tagging app that used to work a few months ago but no longer does. The user authenticates the app and it uploads a picture on their facebook account and tags their friends. The app uploads the picture but stopped tagging for some reason.
require_once('facebook.php');
$_SESSION['init'] = true; $current_date=date('m/d/Y'); $facebook =
new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$facebook->setFileUploadSupport(true);
$session = $facebook->getSession(); $tokenorig =
$facebook->getAccessToken();
$friends =
file_get_contents("https://graph.facebook.com/me/friends?access_token="
. $tokenorig);
$friends = json_decode($friends, true);
$friends = $friends['data'];
foreach($friends as $friend) {
$uids[] = $friend['id'];
}
function makeTagArray($userId) {
$x=1; $y=1;
foreach($userId as $id) {
$tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y);
$x+=1;
$y+=1;
}
$tags = json_encode($tags);
return $tags;
}
$arguments = array(
'message' => 'hi guys ',
'tags' => makeTagArray($uids),
'source' => '#' .realpath('pic2.jpg'),
);
$alb = "13378";
uploadPhoto(
$facebook,
$alb,
$arguments,
$tokenorig);
function uploadPhoto($facebook,$albId,$arguments,$tokenorig) {
//https://graph.facebook.com/me/photos
try {
$fbUpload =
$facebook->api('/'.$albId.'/photos?access_token='.$tokenorig,'post',
$arguments);
return $fbUpload;
} catch(FacebookApiException $e) {
echo "eror";
echo $e;
// var_dump($e);
return false;
}
}
//////////end
First off all,
You need user's to authorize your application to upload photos and tag on behalf of them and
you need publish_stream and user_photos permissions to tag user's friends