I am trying to return user email address using facebook api. Few days ago it was working fine, but today stopped working, and don't know why.
So first I get access token:
FB.login(response => {
if (response.authResponse) {
resolve(response.authResponse.accessToken);
} else {
reject({
error: 'User cancelled login or did not fully authorize.'
});
}
}, {
scope: 'public_profile,email'
});
then I try to get user data
FB.api('/me', {fields: 'first_name,last_name,email'}, function(response) {
console.log(response);
});
but I only get data for first_name and last_name. There is no email.
Also when I ask for permissions, it gives me email as granted, but as I said, email is not returned.
FB.api('/me/permissions', function(response) {
console.log(response);
});
Does anyone knows what can be the problem?
BTW: I am using API v2.4.
FB.api('/me?fields=email,name', function(response) { /* stuff here */ });
Try adding your return fields to your request.
I know this is old question however if someone still seraching for answers then following workaround worked for me.
You need to add email in scope scope="public_profile,email".
<fb:login-button scope="public_profile,email" autologoutlink="false" onlogin="OnRequestPermission();"></fb:login-button>
A possible cause is that the user did not validate the email. Try with another user with validated email to make sure this is not the problem.
See also this answer
If you set all code correctly and still don't get an email from API. The user probably doesn't have an email bound to the account or the email not confirmed by the user.
I made it work using a promise and adding {scope:'email'} to the initial request then including 'email' in the fb.api fields.
fb.login({scope:'email'}).then(function (resp) {
if (resp.status === 'connected') {
fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (FbUserData) {
console.log(FbUserData);
});
}
});
I spent a lot of time trying to figure out why the email is not returned on the FacebookLoginASPnetWebForms Asp.Net application (https://github.com/nickpinheiro/FacebookLoginASPnetWebForms).
I got it working, thanks to N Nem's post above (thanks!).
Open Default.aspx.cs
In HyperLink1.NaviagteUrl, add the query string parameter: &scope=public_profile,email
HyperLink1.NavigateUrl = "https://www.facebook.com/v2.4/dialog/oauth/?client_id=" + ConfigurationManager.AppSettings["FacebookAppId"] + "&redirect_uri=http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/account/user.aspx&response_type=code&state=1&scope=public_profile,email";
I also had to fix some bugs to parse the access token correctly.
This time, the email address is available! I hope this helps?
N Nem above fixed for me. Had the code correct, but missing the scope on the login button. Code I use is below.
function statusChangeCallback(response){
if(response.status === 'connected'){
registerUser(response);
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
function registerUser(statusResponse){
let userId = statusResponse.authResponse.userID;
FB.api('/'+userId+'/?fields=id,email,first_name,last_name',function(response){
if(response && !response.error){
console.log(response);
}
});
}
This is kinda old post but I am sharing the solution that worked for me.
FB Login SDK version: v6.0
First, make sure that the user (which is you for testing purposes) has a valid/verified email. Refer to the code below.
FB.login(response => {
// your code
}, {
scope: 'public_profile,email',
return_scopes: true,
auth_type: 'rerequest'
});
And in getting the user details
const response = await axios({
method: 'get',
url: 'https://graph.facebook.com/v6.0/me?fields=id,name,email&access_token=ACCESS_TOKEN&pretty=0&sdk=joey&suppress_http_code=1'
});
I've added the parameter fields=id,name,email
Related
I am using sailsjs v0.10.5.
I am trying to redirect to login after verifying user email and update the database before redirect.
I am using redirection in my update callback. But it sending the error after updating the database
'Cant send headers after they are sent'.
The following is the code am using for redirection:
verifyEmail: function(req, res){
var userId = req.param('userId');
User.update({id: userId},{isVerified: true}).exec(function(err, user) {
if (!err) {
req.flash('error', 'Your email is verified please login');
res.redirect('/login'); }else { return res.send(user, 400); }
});
Update waterline function is asynchronous, are you sure there isnt some res method later in the scope that may be fired before?
Its recommended to use return res.* for so-called "terminal methods" see http://sailsjs.org/#/documentation/reference/res/res.forbidden.html?q=notes
I am trying to build a comments method within a website without the Facebook comments plugin.
It's a photogallery portion of a website and all the photos are actually hosted on the business Page, I'm doing this to make use of FBs uploading, resizing/rotate etc rather than having to build that myself too.
As a happy bi-product to this I figured I could link comments on the website with comments on FB and have a syncrhonised comments system whereby comments posted on either site (my website or FB) will show equally on either site.
So, I have worked out how to post the comments, but it is always posting as the Business Page, not the logged in user.
This code posts the comment, the variable obj is the ID of the photo on Facebook. msg is obviously the comment text. I have setup methods to get the correct permissions where required which are called if the response from the post states that this is needed.
FB.api('/' + obj + '/comments',
'post',
{ "message": msg,
"access_token": accessToken,
"from": uid
}, // do the post
function (response) { // check the response
if (!response || response.error) { // if there's an error
// DO STUFF WITH ERRORS/REQUEST PERMS ETC
} else {
alert("Msg posted id: " + response.id);
};
});
Everything above works, but when the comment appears, it is always showing from: my Page ID not, what I need which would be from: my user id
accessToken and UID is being retrieved as follows:
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
}
});
The para from in the first function there posts the User ID, NOT the page ID, but whether that para is there or not, it always posts the comment as the Page.
It's frustrating the hell out of me as so far this has been pretty simple to achieve!
I'm using PhoneGap/Cordova with the facebook plugin. Everything seems to work except for getLoginStatus who is not working as defined here and here. When called, it returns some data, but not all: it doesn't return userID nor signedRequest.
Here is the code:
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
var fb_uid = response.authResponse.userID;
var fb_signedRequest = response.authResponse.signedRequest;
alert('logged in');
} else {
alert('not logged in');
}
});
userID is filled with ellipsis (...), while signedRequest is undefined.
I managed to get userID with a graph call to /me:
FB.api('/me', function(me){
if (me.id) {
var fb_uid = me.id;
}
});
I wasn't able to find any way in the documentation to get a signed_request, which I have to use to authenticate the facebook user to a remote service to whom the user already connected to with facebook (I already made a login call so user is OK).
Basically the problem is that my call to getLoginStatus returns
{
status: 'connected',
authResponse: {
session_key: true,
accessToken: 'a long string...',
expiresIn:'a number',
sig: '...', //exactly this string
userID:'...' //exactly this string
secret:'...' //exactly this string
expirationTime:'a long number'
}
}
instead of what documented
As a background, when authentication happens using the plugin then the JavaScript SDK API calls the iOS/Android SDK to handle the authorization and then pass response auth data back to the JS part. The native (iOS/Android) SDKs do not get signed requests back to be able to pass this on to the JS. This is why it's empty.
If you use the latest plugin you should at least now be seeing the user ID. The one from June likely did not pass this back. Otherwise as a work around, you could perform a call to the /me endpoint when authentication is successful in your JS code to get the user id. See the Hackbook example that does this.
I need using javascript to check if the current user is fan of my page.
I goto here https://developers.facebook.com/docs/reference/rest/pages.isFan/. In Test Console I don't understand about Application parameter?
I using this code below, but it's always alert "You like the Application" for any user even guest
window.checkDoesLike = function() {
FB.api({ method: 'pages.isFan', page_id: '138992766181857' }, function(resp) {
if (resp) {
alert('You like the Application.');
} else {
alert("You don't like the Application.");
}
});
};
I've written an extended tutorial about this here.
There are a couple of points to highlight:
You are missing the uid parameter
using if(resp) is not enough
Better code:
FB.api({ method: 'pages.isFan', page_id: 'page_id_here', uid: 'user_id_here' }, function(resp) {
if (resp == true) {
alert('user_id likes the Application.');
} else if(resp.error_code) {
alert(resp.error_msg);
} else {
alert("user_id doesn't like the Application.");
}
});
Quoting:
Once again, you need to check the response of the call if true or
not…using if(resp) is NOT enough! The example in the Facebook
JavaScript Test Console is a bit misleading (the does-like example),
since if you don’t grant the user_likes permission and just try it
with uid: '579187141' it would result that the user likes the
application which is not true!
P.S: I just decremented my user id to get the above user which (it
seems) he has a strict privacy for pages he likes!
This is how I create the login Button (with photo_upload permission):
<fb:login-button autologoutlink="true" perms="publish_stream, offline_access, photo_upload, share_item"></fb:login-button>
Then, I call the following function to retrieve the albums:
function getAlbum()
{
console.log("retrieving the albums");
FB.api('/me/albums', function(response){
console.log(response);
});
return true;
}
but the response object is as follows:
Object
Data: Array[0]
length: 0
I am having 2 public albums, but I cant able to retrieve them with above code. Can someone please tell me what I am doing wrong.
Thanks in advance :)
found the solution. the problem is that I missed the 'user_photos' permission.
Now, everything works fine :)