login with facebook, facebook give me other id - facebook

i try to do a login with facebook, i build this code:
<?php
ob_start();
include("facebook_constants.php"); // in this file i have appid and appsecretid
$users = $facebook->getUser();
if ($users!="") {
try {
$user_profile = $facebook->api('/me','GET');
$logoutUrl = $facebook->getLogoutUrl();
$fuserid=$user_profile["id"]; // give me the id of the user
$fusername=$user_profile["name"]; // give me the name of the user
$femail=$user_profile["email"]; // give me the email of the user
$fusernamenew = str_replace(' ', '', $fusername); // change the name to username (omer lol = omerlol)
$newtoken=base64_encode($fuserid."::".$fusernamenew."::".$femail); // data to token
$itsme = $this->haimz->DB->num_rows("SELECT id FROM users WHERE fbid = ?haimz?", $fuserid); // if have user with that id = login , if not goto register page
if($itsme > 0){
echo "goto login page";
}
else{
echo "goto register page";
exit;
}
} catch (FacebookApiException $e) {
$users = null;
}
}
?>
the $fuserid give me other userid but the $fusername and $femail give me the correct .
its not the userid of the user.
its other userid.

https://developers.facebook.com/docs/apps/changelog
To better protect people's information, when people log into a version of your app that has been upgraded to use Graph API v2.0, Facebook will now issue an app-scoped ID rather than that person's orginal ID. However, for users that have previously logged into your app, the user ID will not change.
You don´t get the "real" ID of a user anymore, for privacy reasons. You only get an App Scoped ID that is unique in the App only. You can still use it to identify the user when he returns to your App with the App Scoped ID, it will only be different in another App.
You can also map App Scoped IDs between different Apps with the Business Mapping API, if needed: https://developers.facebook.com/docs/apps/for-business

Related

Unable to get friends despite permissions

I am making an application which needs to get friends of the current user
if ($user) {
$user_profile = $facebook->api('/me');
$friends= $facebook->api('/me/friends');
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array('scope' => 'user_status,publish_stream,user_photos, user_friends');
$loginUrl = $facebook->getLoginUrl( $params);
}
Now, the problem I am facing is that the friends list returns empty for my current app. For an older app, albeit it works fine. The new app is public and I checked in the Status and Reciew area where it says that the app has user_friends permission by default. I can't seem to understand what's wrong with this/
Per https://developers.facebook.com/docs/apps/changelog#v2_0_login, user_friends now only gives access to friends using the same app, not all friends.

Checking if user is a member of a specific facebook group

Do you have an example of how to confirm that the person trying to access the content of your app is a member of a specific facebook group, using php? I've read through the developer docs and all it managed to do was confuse me even more. I've seen posts similar to this one but they only show me how to print a list of members...
I need to know how to get the list and see if the user trying to access the app is a member of the group so that I can either a: allow access or b: deny access and re-direct the user to the group page so they can request membership...
Yes its pretty simple, you just need to know the ID of the group you want to do that and ask for the scope user_groups permission, so you can query the groups the user have.
With the PHP SDK you can do this:
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me?fields=id,name,groups','GET');
$user_belongs_to_group= FALSE;
foreach($user_profile['groups']['data'] as $group){
if($group['id']==YOUR_GROUP_ID)
$user_belongs_to_group=TRUE;
}
if($user_belongs_to_group){
//Alright! the user belongs to the group
}
else{
//Oh snap you don't belong here, but you can if want to
}
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
Some of this code I grabbed from the facebook, you can see the data that is returned when query for groups here: https://developers.facebook.com/tools/explorer
Press the plus sign after the name go to connections, choose groups and press send
You are in a catch-22, you will need the user to grant permission to his groups via the user_groups permission.

First time access after facebook authentication

Is there any way that when user authenticate my app through Enhanced Auth Dialog, and gets redirected to canvas page, my canvas page knows its his first time visit?
Your may get the user id from facebook after the user redirected to canvas page:
Here is the example:
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$app->user_id = $facebook->getUser();
After you obtain the user_id, you may save it in your database so that you can know whether the user is first time visit.
The previous answer is correct and here is the code I use to check if they are a new user or a returning user. Then I insert them into the database if they are new.
// Get the app User ID
$user = $facebook->getUser();
$seeif = mysql_num_rows(mysql_query("SELECT id FROM users WHERE fbid='$user' LIMIT 1"));
if ( $seeif == "1" ) {
// already exists
} else {
// insert new user
mysql_query("INSERT INTO users (fbid) VALUES('$user' ) ")
or die(mysql_error());
}
While the previous answers are correct, this might be more useful to you in the long run building your application.
Remember this relies on you having an open database connection as well as the facebook php sdk
// this brings back more info print_r($user)
$user = $facebook->api('/me');
$exists = mysql_num_rows(mysql_query("SELECT id FROM users WHERE fbid='" . $user['id'] . "'));
if ( $exists > 0) {
// already exists
} else {
// insert new user
mysql_query("INSERT INTO users (fbid) VALUES('" . $user['id'] . "' ) ") or die(mysql_error());
// do something else (post to timeline?)
// look up facebook feed for more info on posting
try {
// Post Message to feed.
$facebook->api('/me/feed', 'POST', $msg);
} catch (FacebookApiException $e) {
error_log($e);
}
}

I want to get user access token rather than application access token

I want to get user access token because I need to get user's posts and comments.
When I use Graph API Explorer, the access token it generates is correct one and shows me my posts and comments and some other data. But when try to get posts and comments by using this code than it does not return me posts and comments and return some other data only which i don't need.
require_once('facebook.php');
$config = array(
'appId' => '383128895071077',
'secret' => '6a9ab479186f53db5c531a3fa5f91be0',
);
$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
$result = $facebook->api('/me/feed/', array('access_token' => $access_token));
I searched all the google and get access token by different ways but I could not get posts and comments of me. I must be doing something wrong and need to sort out this as soon as possible.
Thanks in advance.
You should check this example
Facebook php sdk example
you should check whether we have user access token like this
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} 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();
} else {
$loginUrl = $facebook->getLoginUrl();
}
Once we get the user details, you can ensure that user is logged into app and authorized the app.
For feeds
You should have extended permissions read_stream
then you can read the wall feed.

facebook : load the wall content of a facebook page in a website

I have a website and a facebook page for the website. For most of the news update, I post them on the facebook page's wall.
Now I want to show the wall's content of that facebook page on my website, just like the facebook's Like Box with "stream" enabled: http://developers.facebook.com/docs/reference/plugins/like-box/
However, I only want to show the stream, and perfectly can show it in my own presentation. So is it possible to get only the content of a facebook page by facebook's API?
I think if you use the SDK you can get it by using
fb->api("/{id}/feed");
There's a php and a javascript sdk.
But you also need an access token now to get the feed of a page.
EDIT: Here's a copy of example.php (from the php-sdk) modified for your purpose.
include_once("facebook-sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID GOES HERE',
'secret' => 'SECRET GOES HERE',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
// This is where we grab the posts
$wall_posts = $facebook->api('/courseyou/posts');
} 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();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>Login with Facebook</div>
<?php endif ?>
<h3>Wall Posts</h3>
<?php
foreach ($wall_posts["data"] as $post) {
echo "<p>".$post["from"]["name"].": ".$post["message"]."</p>";
}
?>
As far as I know you need an access token to view a page's posts/feed with the api, which makes me think that you need the user to login with facebook.... I'm fairly new to this, but you should look into how to get an access token, because you need one for this.