In this script
function fql(){
FB.api('/me', function(response) {
var query = FB.Data.query('select uid, name, email from user where uid={0}', response.id);
query.wait(function(rows) {
document.getElementById('name').innerHTML =
'Your name: ' + rows[0].name + "<br />" +
+ rows[0].email + "<br />" + rows[0].uid;
});
});
}
Function returns name and pic, but email value is NaN. App requires basic data and email (it was set in app properties/Auth dialog) but i cant get user email.
When I tested this query in Facebook test console, I've got email field with correct value.
Check that in Test Console and on your site you use the same application id and ensure that user have granted the email permission to your application!
This can be done with simple Graph API call:
FB.api('/me/permissions', function(response){
if (response && response.data){
var userPermissions = response.data.shift();
if (userPermissions.email){
alert('User is granted email permission');
} else {
alert('User is NOT granted email permission');
}
} else {
// No response or results from the Graph API
}
});
Couple of things you should be aware of!
FB.Data.* is gone! It's deprecated, undocumented and should not be used anymore! It's just a matter of time for it to disappear from the JavaScript SDK code...
You just doesn't want extra call just to figure the id of current user (most chances that you already have it, just check FB.getUserID()), in FQL use me() to refer to current user.
Rewrite your usage of FB.Data.* and query.wait with:
FB.api('/fql', {q:'YOUR_FQL_QUERY_HERE'}, function(response){
var rows = response.data;
});
In your case same results may be received by Graph API call without usage of FQL:
FB.api('/me', {fields: 'id,name,email'}, function(response){
// response -> {id: UID, name: 'User Name', email: 'user#email.dz'}
console.log(response);
});
Related
I'm creating a game using Parse.com as back-end. I want to create another user in Parse database while I'm still logged in. I'm using Parse user.signUp() method to register another user, since Parse.com provides it as a standard method for user registration. But I don't want the current user's session to expire. Currently, it works but as soon as another user is registered, the new user's session is directly logged in.
Here is the case for your information:
I want the user (e.g. myself here) to enter the email for user to invite other user to this game. When user enters email id, an email is sent actually (using PHP) and then the function creates a temporary user. For this I'm using the following code:
function inviteFriend() {
var email = $("#inviteEmail").val();
var pass = 'randomPass123';
var varData = 'email=' + email;
console.log(varData);
if (email != null) {
$.ajax({
type : 'POST',
url : './src/php.php',
data : varData,
success : function() {
alert("Invite sent successfully.");
var user = new Parse.User();
user.set("username", "tempUser");
user.set("password", pass);
user.set("email", email);
user.signUp(null, {
success : function(user) {
alert("User created successfully.");
console.debug(user);
},
error : function(user, error) {
console.log("LogIn error := " + error.message);
}
});
}
});
}
}
Please suggest any workarounds.
I am already using this script but I didn't get hometown and location when I use API the hometown and location is undefined message appears please give example code and how to get user mobile number in Facebook API:
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
testAPI();
} else {
// cancelled
}
}, { scope: 'email,user_birthday,user_location,user_hometown' });
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.' + ' Email: ' + response.email + ' Facebook ID: ' + response.id);
//console.log('Good to see you, ' + response.name + '.');
var userfirstName=response.first_name;
var lastName=response.last_name;
var useremail=response.email;
var usersex=response.gender;
var userbithday=response.birthday;
var hometown= response.hometown.name;
var location= response.location.name;
alert(hometown);
});
}
<fb:login-button show-faces="false" width="200" max-rows="1" scope="email,user_birthday,user_location,user_hometown" onclick="testAPI();" onlogin="Log.info('onlogin callback')">
Sign Up with Facebook
</fb:login-button>
but can't get hometown and location, please help give any example.
You are using wrong permissions, try this:
{ scope: 'email,user_birthday,user_location,user_hometown' }
Another thing, to access the hometown use this:
var hometown= response.hometown.name;
Also, instead of /me; mention explicitly the fields you want using the fields parameter; just like: /me?fields=hometown
Graph API Explorer
I have created a custom action that I want to allow users to post to their timeline with e.g. {User} read {Article}. I can get this to work fine when I do a post using the sample URL like this :
FB.api('/me/fbsite:actionname' +
'?object=http://samples.ogp.me/356694057772404&access_token=abc','post'
But when I replace the sample URL with my own URL I get the error 'The session was invalidated explicitly using an API call'. Can anybody please advise?
EDIT : Changed my Javascript to this, but I now get 'Invalid OAuth Access Token' :
<script type="text/javascript">
function redeem() {
FB.api('/me/fbsite:actionname' +
'?object=http://samples.ogp.me/356694057772404&access_token=\' + <%=UserManager.Current.FB.accessToken %> + \'', 'post',
function (response) {
var msg = 'Error occured';
if (!response || response.error) {
if (response.error) {
msg += "\n\nType: " + response.error.type + "\n\nMessage: " + response.error.message;
}
alert(msg);
}
else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
That might be because you are using the same access token in same session. So try logging out and log back in, get a new FB access token, and try the query using the new access token you got, that should work as expected.
I have integrated Facebook login in my website using javascript SDK.. Everything is working fine except for the auth dialog box, which is showing only once for a user.Next time when the user logs it redirects without showing the auth dialog box.I want to show the auth dialog box each time the user logs.
This is the code I am using
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxx',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
if (window!=window.top) {
FB.Canvas.setAutoResize();
};
FB.getLoginStatus(function(response) {
if (response.authResponse) {
window.FBlogin = function(){
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
var query = FB.Data.query('select name,username, email, hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
//document.getElementById('name').innerHTML = '<img src="' + rows[0].pic_square + '" alt="" />';
var name=rows[0].name;
var email=rows[0].email;
var uid=response.id;
var username=rows[0].username;
var pic=rows[0].pic_square;
});
});
}
else {
alert("error");
}
}, {scope: 'email'});
};
}
else {
var authbox = document.getElementById('FBauth');
authbox.innerHTML="";
var a = document.createElement('a');
a.setAttribute("href","javascript:void();");
a.setAttribute("onclick","FBlogin();");
authbox.appendChild(a);
window.FBlogin = function(){
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
var query = FB.Data.query('select name,username, email, hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
var name=rows[0].name;
var email=rows[0].email;
var uid=response.id;
var username=rows[0].username;
var pic=rows[0].pic_square;
});
});
}
}, {scope: 'email'});
};
}
});
FB.Event.subscribe('auth.login', function () {
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
var query = FB.Data.query('select name,username,email, hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
var name=rows[0].name;
var uid=response.id;
var email=rows[0].email;
var username=rows[0].username;
var pic=rows[0].pic_square;
var page='';
$('<input />').attr({'type':'hidden', 'id':'fbname','value':email}).appendTo('#fbaccess');
});
})
});
FB.Event.subscribe('auth.logout', function(response) {
});
};
Everything is working fine except for the auth dialog box, which is showing only once for a user.Next time when the user logs it redirects without showing the auth dialog box.
If the user is already connected to your app and has given all of the requested permissions – then this is the intended behavior.
Calling FB.login again in this situation will open the popup, and close it again immediately.
I want to show the auth dialog box each time the user logs.
I can’t see the benefit of an app behaving in such a way.
Anyways, there is a parameter to explicitly request that the user re-authenticate: auth_type=reauthenticate. This will force the user to re-enter their password. See https://developers.facebook.com/docs/authentication/reauthentication/#client-side for details.
But I’m not sure that this is what you actually want.
Another way could be to delete permissions via your app, and then have them requested again via the scope parameter. That should bring up the Auth dialog again. For details on that, see https://developers.facebook.com/docs/reference/api/user/#permissions
I have a javascript function that uses fb.login to retrieve the users info and checks to see if the user likes my fb page. fb.login causes a pop up where the user must click a login button. If the user likes my page i need to first create a cookie then redirect them to the main app:
function checkUser() {
var page_id = "my id goes here"; //
FB.login(function (response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + response.id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == response.id) {
//$("#container_like").show();/
//set cookie
document.cookie = "fbId=" + response.id;
window.location = "/kisses.aspx";
} else {
$("#likepageholder").show();
//$("#container_notlike").show();
//window.location = "/kisses.aspx";
//and here you could get the content for a non liker in ajax...
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}
If there a way around using fb.login to do this?
Nope. Unfortunately, you need the user to log in (and grant your app basic permissions [based on the appId you use in FB.init) in order to query the user's likes.
That being said, it looks like your code should work.