I am using FB SDK v2.8. What I tried doesn't show user's email. It doesn't matter if I log in with my mobile phone, or email address. How can I get the user's email?
In my fb app dashboard, I can see email permission in approved items section
<div class="fb-login-button" data-max-rows="1" data-size="large"
data-show-faces="false" data-auto-logout-link="true"></div>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = function() {
FB.init({
appId : 'app_id',
cookie : true,
xfbml : true,
version : 'v2.8'
});
FB.AppEvents.logPageView();
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
....
} else if(response.status === 'not_authorized'( {
.... }
else {
... }
});
function getCurrentUserInfo() {
FB.api("/me", {
locale : 'en_US',
fields : 'name, email, gender, birthday, currency, website'
}, function(response) {
console.log(response);
console.log(response.name + ': ' + response.email);
}
});
It's weird to see a downvote with no explanation.
You are asking only for the default public_profile permission, you have to ask the email permission also.
Check the documentation here
FB.login(function(response) {
console.log(response);
}, {scope: 'email'});
Related
i am struggling to ad the post to wall functionality to an existing piece of code ?
It works fine with logging in and obtaing email for form submit, i would like it to post to wall on log in as well ! in the simplist way possible .?
URL to see it in action is http://lproctor.co.uk/.
// The facebook JavaScript SDK to log in to FB
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
FB.ui(
{
method: 'feed',
name: 'The Facebook SDK for Javascript',
caption: 'Bringing Facebook to the desktop and mobile web',
description: (
'A small JavaScript library that allows you to harness ' +
'the power of Facebook, bringing the user\'s identity, ' +
'social graph and distribution power to your site.'
),
link: 'https://developers.facebook.com/docs/reference/javascript/',
picture: 'http://www.fbrell.com/public/f8.jpg'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
NOW USING
<script type="text/javascript">
// The facebook JavaScript SDK to log in to FB
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.ui(
{
method: 'feed',
name: 'Facebook name',
link: 'http://lproctor.co.uk/',
picture: 'http://lproctor.co.uk/logo.jpg',
caption: 'facebook caption',
description: ' facebook description facebook description facebook description facebook description',
message: 'Facebook message!'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
Try something like that
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
} else if (response.status === 'not_authorized') {
// not_authorized
login();
} else {
// not_logged_in
login();
}
});
};
Here is my code which is throwing Uncaught ReferenceError: FB is not defined. It works fine synchronously if I remove window.fbAsyncInit method and add <script src="//connect.facebook.net/en_US/all.js"></script> in <head>
<!doctype html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script type="text/javascript">
function login()
{
FB.login
(
function( response )
{
if ( response.authResponse )
{
FB.api
(
"/me",
function( response )
{
document.getElementById( "profile_name" ).innerHTML = response.name;
document.getElementById( "list" ).innerHTML = "http://graph.facebook.com/" + response.id + "/friends";
}
)
}
}
);
}
function getFriends() {
alert('1');
FB.api('/me/friends', function(response) {
//alert('2:'+response.data);
if(response.data) {
$.each(response.data,function(index,friend) {
//alert(friend.name + ' has id:' + friend.id);
$( "list" ).append = response.name;
});
} else {
alert("Error!");
}
});
}
</script>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init
(
{
appId : "109036532604620",
channelUrl:"http://localhost/testsaav/channel.html",
status : true,
cookie : true,
oauth : true
}
);
};
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert('connected');
} else if (response.status === 'not_authorized') {
// not_authorized
} else {
// not_logged_in
}
});
(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ true));
</script>
Login
<img id="profile_pic"/>
<pre id="list"></pre>
<div id="profile_name"></div>
</body>
</html>
Try moving
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert('connected');
} else if (response.status === 'not_authorized') {
// not_authorized
} else {
// not_logged_in
}
});
this code after the FB.init function but inside the window.fbAsyncInit function e.g
window.fbAsyncInit = function()
{
FB.init
(
{
appId : "109036532604620",
channelUrl:"http://localhost/testsaav/channel.html",
status : true,
cookie : true,
oauth : true
}
);
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert('connected');
} else if (response.status === 'not_authorized') {
// not_authorized
} else {
// not_logged_in
}
});
};
You'll notice from the sample at https://developers.facebook.com/docs/reference/javascript/ that your initialization code should go after the init method within the asyncinit method to function correctly. This ensures things are called in the correct order once the SDK has been loaded
https://developers.facebook.com/docs/reference/javascript/
I have a registration form that uses an old version of the FB API.
I am trying to update it to the current version and am having problems dealing with the response from FB.
After clicking the FB login button and granting permissions (email,publish_stream) I cannot find a way to know that the user has granted access (and not just logged in as an already registered user) and needs to proceed to the next page of the registration form.
The response after a successful permission request is the same as a successful login:
{
authResponse:
{
accessToken: AAAFEIew...URYo,
userID: 100003908595992,
expiresIn: 6014,
signedRequest: uhTqZodDn0QUoWAUBuYEKmlaM8zViO0r_fnKONaC4v8.eyJhbGdvcml...k5MiJ9,
},
status: connected
}
How can I tell if the login was the result of an already registered user or a new registration?
I am using the javascript SDK.
Code:
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<div id="fb-root" style="padding: 0px;"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'API_KEY',
status : true,
channelUrl : '//'+window.location.hostname+'/channel.php',
cookie : true,
xfbml : true,
oauth : true
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
alert('logged in!');
}
else
{
// user has not auth'd your app, or is not logged into Facebook
alert('not logged in!');
}
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
This is the resource I used when attempting to do what you are doing. It works extremely well.
window.fbAsyncInit = function() {
FB.init({ appId: 'Your_APP_ID',
status: true,
cookie: true,
xfbml: true,
oauth: true});
showLoader(true);
function updateButton(response) {
button = document.getElementById('fb-auth');
userInfo = document.getElementById('user-info');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function(info) {
login(response, info);
});
button.onclick = function() {
FB.logout(function(response) {
logout(response);
});
};
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Login';
button.onclick = function() {
showLoader(true);
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(info) {
login(response, info);
});
} else {
//user cancelled login or did not grant authorization
showLoader(false);
}
}, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
Reference: http://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/
Faith Sloan
I realized that I needed to use the getLoginStatus function in the FB SDK.
My functioning code is:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo HSWI_FB_KEY; ?>',
status : true,
logging : true,
channelUrl : '//'+window.location.hostname+'/channel.php',
cookie : true,
xfbml : true,
oauth : true
});
FB.Event.subscribe('auth.login', function(response)
{
window.location.href = '/login_script.php';
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
checkLoggedInFB();
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
function fbLogin()
{
FB.login(
function(response)
{
if(response.authResponse)
{
window.location.href = '/login_script.php';
}
}
);
}
function checkLoggedInFB()
{
try
{
FB.getLoginStatus(
function(response)
{
if(response.status == 'connected')
{
window.location.href = '/login_script.php';
}
}
);
}
catch(e)
{
alert('Error checking if logged into FB: '+ e.message);
return false;
}
return true;
}
</script>
<style type="text/css">
.fb-login-button
{
margin-top: -15px;
}
.fb-login-button.fb_iframe_widget.fb_hide_iframes
{
display: none;
}
</style>
<div class="fb-login-button" scope="email,publish_stream,user_birthday,user_location,user_about_me">Login</div>
FB.ui(
{
method: 'feed',
name: 'some text',
link: 'some text',
picture: 'aa.jpg',
caption: 'some text',
description: 'some text',
message: 'some text'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
});
}
That code WORK fine, now i like after :
alert('Post was published.');
to be logged out from facebook, silently
HOW ?
Adding that code After the alert('post publish') did not do anything !!
FB.ui(
{ method:'auth.logout', display:'hidden' },
function() { alert("you're logged out!"); }
);
i have found : FB auth.logout is being raised after being logged in using the "server-side-workflow" (OAuth 2.0) but not sure i understand the code enough to know it do what i ask !
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
https://developers.facebook.com/docs/reference/javascript/FB.logout/
Best Practices
FB.logout will log the user out of both your site and Facebook. You
will need to have a valid access token for the user in order to call
the function.
Calling FB.logout will also invalidate the access token that you have
for the user, unless you have the offline_access permission.
I wrote a sample using the comments box to fire the auto logout
http://shawnsspace.com/fb.logout.test.php
THE CODE:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '112104298812138',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
//channelUrl : 'http://WWW.MYDOMAIN.COM/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
FB.Canvas.EarlyFlush.addResource("http://shawnsspace.com/index.php");
FB.Canvas.setAutoResize();
FB.getLoginStatus(function(response) {
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
} else {
}
});
FB.Event.subscribe('comment.create', function(response) {
//alert(JSON.stringify(response));
FB.logout(function(response) {
window.location.reload();
});
});
FB.Event.subscribe('auth.login', function(response) {
//top.location.href = 'http://apps.facebook.com/shawnsspace/fbcomments.php?ref=loggedin';
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
//top.location.href = "http://apps.facebook.com/shawnsspace/fbcomments.php?ref=loggedout";
alert('logged out');
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
I am using facebook javascript SDK to post comment on facebook and using this code:
FB.init({
//appId: 'YOUR_APP_ID', cookie: true,
appId: '2697XXXXXXXXXX098', cookie: true,
status: true, xfbml: true
});
FB.ui({
method: 'feed',
name: 'MyComment',
link: 'http://www.facebook.com./',
picture: 'http://xyz.com/App_Shared/Images/logo.jpg',
caption: 'Comment caption',
description: 'comment description',
message: 'Join facebook.'
}, function (response) {
if (response && response.post_id) {
alert('Post was published.' + response.post_id);
} else {
alert('Post was not published.');
}
});
and now i want to delete this post by using the same type of js code.
I saved post_id in my database.
Can you provide me the code or any URL to do this.....
First of all you must give to access to you application and allow by user according to this URL
http://developers.facebook.com/docs/reference/api/permissions/
These permission you can use like this method
FB.login(function (response) {
if (response.authResponse) {
FB.api('/me', function (response) {
jQuery('#hdfUID1').val(response.id);
jQuery('#btnFBLoginTemp').click();
// FB.logout(function (response) {
// });
});
} else {
jQuery('#hdfUID1').val('');
}
}, { scope: 'email,user_photos,read_stream,publish_stream,offline_access' });
if user allow these permissions then you can delete comment from user's wall which was posted by your application by using "Anatoly Lubarsky" answer method....
FB.api(postId, 'delete', function(response) {
// callback here
});
should be very simple like:
FB.api(postId, 'delete', function(response) {
// callback here
});
hope this helps