I am trying to use facebook Graph API to fetch user details.
Below is my FB.api query :
FB.api('/me', 'GET', { access_token: token, fields:'id,name,email,gender,location,likes' }, function(response) {
console.log(response);
alert(response);
});
With above query i am able to fetch id, name and gender but not location, email and likes.
Version for FB SDK : v2.5
I think the issue is related to some permissions required to fetch such private data of user.
Please suggest what need to be done to get all the data.
You need to ask for the permissions in the login process, with the scope parameter. Here is a list of those: https://developers.facebook.com/docs/facebook-login/permissions
Here´s how to use FB.login with additional permissions:
FB.login(function(response) {
if (response.authResponse) {
//user just authorized your app
document.getElementById('loginBtn').style.display = 'none';
getUserData();
}
}, {scope: 'email,user_likes,user_location'});
Source: http://www.devils-heaven.com/facebook-javascript-sdk-login/
Make sure you read about Login Review too: https://developers.facebook.com/docs/facebook-login/review
Related
Sorry this question is kind of long, it's because I've been trying to solve this problem for a while and want to make sure I don't leave any info out. I'm building a Cordova app and using Firebase for the authentication/database back end. I've been trying to authenticate users into Firebase using a Log in with Facebook button for almost a week now, but I haven't been able to get it to work.
Originally I tried following Firebase's example here: https://firebase.google.com/docs/auth/web/facebook-login (I need to use the "Advanced: Handle the sign in flow manually" as it is a Cordova Android & iOS app), this example didn't work for me as the link to Facebook's SDK script (//connect.facebook.net/en_US/sdk.js) kept throwing the error:
file://connect.facebook.net/en_US/sdk.js Failed to load resource: net::ERR_FILE_NOT_FOUND
I tried to fix this error in several ways, such as:
Changing it to https://connect.facebook.net/en_US/sdk.js (this resulted in the error: Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings. )
Added the links in question to the list of "Valid OAuth redirect URIs" and domains in the Facebook app settings
Storing the file in my local file system (and locally inside the app on my phone)
Including the entire SDK inside the head of my index.html file
None of these attempts worked. So instead I decided to use the plugin cordova-plugin-facebook from here: https://github.com/bisrael/cordova-plugin-facebook
This is the code I'm using to get the user's information from Facebook with the plugin:
function logInWithFacebook(){
CordovaFacebook.login({
onSuccess: function(result) {
console.log(result);
console.log(result.authToken);
// Store or send the user auth/access key here?
// Get user's name
retrieveUserDetails();
if(result.declined.length > 0) {
alert("The User declined something!");
}
},
onFailure: function(result) {
if(result.cancelled) {
alert("The user doesn't like my app");
} else if(result.error) {
alert("There was an error:" + result.errorLocalized);
}
}
});
}
function retrieveUserDetails(){
// Now that the user has authroised the app, make request to CordovaFacebook plugin to get user's name
CordovaFacebook.graphRequest({
path: '/me',
params: { fields: 'name' },
onSuccess: function (userData) {
console.log(userData);
console.log(userData.name);
// Here somehow send the retrieved username and send it to the Firebase function so that it's linked with the auth key.
},
onFailure: function (result) {
if (result.error) {
Error.log('error', 'There was an error in graph request:' + result.errorLocalized);
}
}
});
}
I'm now able to click on a log in button and log in successfully through Facebook. That process is returning a user auth/access key and the user's name from Facebook.
As I understand it, the manual log in flow example in Firebase's docs (https://firebase.google.com/docs/auth/web/facebook-login) takes the key returned from Facebook, converts it into a Firebase key, and then enters the user's newly created Firebase key and their username into Firebase's servers.
This seems pretty straight forward in the following sample code:
function checkLoginState(event) {
if (event.authResponse) {
// User is signed-in Facebook.
var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(event.authResponse, firebaseUser)) {
// Build Firebase credential with the Facebook auth token.
var credential = firebase.auth.FacebookAuthProvider.credential(
event.authResponse.accessToken);
// Sign in with the credential from the Facebook user.
firebase.auth().signInWithCredential(credential).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
} else {
// User is already signed-in Firebase with the correct user.
}
});
} else {
// User is signed-out of Facebook.
firebase.auth().signOut();
}
}
function isUserEqual(facebookAuthResponse, firebaseUser) {
if (firebaseUser) {
var providerData = firebaseUser.providerData;
for (var i = 0; i < providerData.length; i++) {
if (providerData[i].providerId === firebase.auth.FacebookAuthProvider.PROVIDER_ID &&
providerData[i].uid === facebookAuthResponse.userID) {
// We don't need to re-auth the Firebase connection.
return true;
}
}
}
return false;
}
FB.Event.subscribe('auth.authResponseChange', checkLoginState);
My question is, how can I send the auth key and username returned from the Cordova plugin code, to Firebase's example code so that it works smoothly?
Firebase's example code includes this listener which listens for any change in the Facebook authorization status: FB.Event.subscribe('auth.authResponseChange', checkLoginState); but as this uses Facebook's SDK it won't work with my current set up.
I'm using the following Firebase chat app as a template to work from: https://gist.github.com/puf/8f67d3376d80ed2d02670d20bfc4ec7d as you can see it has a Login with Facebook button, but no code for handling the process, I'm trying to apply parts of the manual log in flow example in Firebase's docs (https://firebase.google.com/docs/auth/web/facebook-login) with data returned from the cordova-plugin-facebook queries, and integrate both with Firebase's chat app template.
I'm really at a loss as to what to do next, I've tried everything I can think of. Any help in solving this problem would be really, really appreciated.
Thank you in advance!
UPDATE
Questions and answers:
How does it work at the moment?
Right now I have a "Facebook Login" button - when this is clicked it runs logInWithFacebook(). This function uses the CordovaFacebook plugin, it also runs the function retrieveUserDetails() after the user signs in with Facebook. retrieveUserDetails() gets some user info from Facebook which I hope to then insert into my Firebase database.
logInWithFacebook() works correctly (it opens up a Facebook login page, and when the user logs in, I'm able to console.log the user's Facebook ID, and the Facebook access Token.
retrieveUserDetails() also works correctly (I'm able to console.log the user's name taken from Facebook).
How do you want it to work?
I'm happy with how the first half of the process is working (the logging in with Facebook and retrieving user details is working correctly). However I want this log in to trigger Firebase's auth state change listener, so that Firebase detects and confirms that the user has logged in:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log("User is signed in.");
} else {
console.log("User is not signed in.");
}
});
What is not working the way you want?
The first half of the process is working correctly, but I'm lost when it comes to what to do with the accessToken returned from Facebook. From reading the docs I think that Firebase is supposed to convert this token into a Firebase access token, and then that is used to log the user into Firebase (this would also trigger the above AuthStateChanged function). From there I want to be able to insert any data I've retrieved from Facebook (the user's name etc) into my Firebase database. But the main problem is getting the Facebook accessToken converted into a Firebase login (the second block of code in my original question is where I'm trying to perform the conversion/sign into Firebase).
Because I'm using Cordova, this method (logging into Facebook with a plugin and then handling the conversion of the accessToken) seems to be the only way to log in with Facebook. But I'm totally lost on how to complete the second half.
UPDATE 2
I've trimmed parts from the sample convert-Facebook-token-to-Firebase-token code from the docs so that the Facebook SDK isn't required. And it appears to be working. This is the code after cutting away the SDK related parts:
// First, define the Facebook accessToken:
var FBaccessToken = result.accessToken;
// Build Firebase credential with the Facebook auth token.
var credential = firebase.auth.FacebookAuthProvider.credential(
FBaccessToken);
// Sign in with the credential from the Facebook user.
firebase.auth().signInWithCredential(credential).then(function(user){
console.log("It looks like we signed into Firebase with the Facebook token correctly.");
}, function(error) {
console.log("Something went wrong, user isn't signed into Firebase with the FB token.");
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
I still need to add the user's email from Facebook and try to send that while logging into Firebase too - so that I'll have some identifier for the user in the Firebase console, but this is a good start.
2nd UPDATE
The below code successfully gets user data from Facebook after the user authorizes the app:
CordovaFacebook.graphRequest({
path: '/me',
params: { fields: 'first_name,last_name,email,locale,gender,age_range,picture.width(200).height(200)' },
onSuccess: function (userData) {
console.log(userData)
var first_name = userData.first_name;
var last_name = userData.last_name;
var email = userData.email;
var locale = userData.locale;
var gender = userData.gender;
var min_age = userData.age_range.min;
var profile_picture = userData.picture.data.url;
// Enter user details into the Firebase database:
firebase.database().ref('users/' + uid).set({
first_name: first_name,
last_name: last_name,
email: email,
locale: locale,
gender: gender,
min_age: min_age,
profile_picture : profile_picture
});
console.log("Facebook user data should now be in the database!");
},
onFailure: function (result) {
if (result.error) {
Error.log('error', 'There was an error in graph request:' + result.errorLocalized);
}
}
});
(Just an answer to the last update, as you figured out the rest :))
How to get user email from CordovaFacebook.login()
Looking at the CordovaFacebook documentation you can add a permissions property on the object passed to the login method.
According to the Facebook API documentation the permission name for email is just "email".
I haven't tested, but I think this should work:
CordovaFacebook.login({
permissions: [ 'email' ],
onSuccess: function(result) {
console.log('email:', result.email);
...
},
onFailure: function(result) {
...
}
});
I currently have a Facebook app written with app that uses firebase to authenticate and login to my app. I am getting a access token with the firebase auth. I wanted to use this token to make graph api calls like
FB.api(
'/me/albums',
'GET',
{},
function(response) {
// Insert your code here
console.log(response);
console.log(token);
}
);
I am following the documentation on firebase to make the authentication. The user is successfully created on firebase the user has given permission for photo access. I just am not able to figure out how to use the token to makes calls to Facebook's graph api.
var provider = new firebase.auth.FacebookAuthProvider();
provider.addScope('user_photos');
provider.addScope('user_friends');
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Facebook Access Token. You can use it to access the Facebook API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
EDIT
When I make the call after the redirect I get the following error
"An active access token must be used to query information about the current user."
You could do what #luschn said or you could also make a simple http request with facebook api.
$.get(
"https://graph.facebook.com/me",
{
'fields' : fields,
'access_token' : token
},
function(response) {
//enter code here
}
)
You can get the fields from facebook's graph api and access token is the one you got from firebase.
While working with the JS SDK, you don´t have to deal with the User Token after authorization. I am not sure how it works with Firebase, but i assume that you have to add the Token on your own if you want to use the JS SDK after login with Firebase:
FB.api(
'/me/albums', {fields: '...', access_token: token}, function(response) {
console.log(response);
}
);
Also, make sure the Access Token is valid and includes the user_photos permission. You can debug it here: https://developers.facebook.com/tools/debug/accesstoken/
You can also try using the Fetch API instead of the JS SDK to make API calls with the Token from Firebase: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
I am setting up a Facebook login, and am successfully getting things like first_name, email, etc. However, I cannot seem to figure out how to get birthday. If I call for birthday as below, nothing returns.
FB.api('/me', {fields: 'birthday'}, function(response) {
console.log(JSON.stringify(response));
})
If I call user_birthday as below, I get this error: "error":{"message":"(#100) Tried accessing nonexisting field (user_birthday)
FB.api('/me', {fields: 'user_birthday'}, function(response) {
console.log(JSON.stringify(response));
})
How do I do this?
Since you did not post your login code and you did not mention permissions, i assume you forgot one important thing: Authorizing with the user_birthday permission.
FB.login(function(response) {
if (response.authResponse) {
//user authorized the app
}
}, {scope: 'user_birthday', return_scopes: true});
user_birthday is the permission, birthday is the name of the field. If it still doesn´t work, then there is most likely no birthday set. Not sure if it is required.
For example: http://www.devils-heaven.com/facebook-javascript-sdk-login/
I am trying to get User data(/me) & Friends data(/me/friends) using a function which fires after login through facebook. I am using Javascript SDK
//Getting logged in user data//
function users(callback) { FB.api('/me?fields=name,first_name,username,birthday,hometown,albums,photos,location,picture.height(100).width(100),cover', function(response) {
callback(response);
});
}
//Getting logged in user friends data//
function pic(album_id, callback) {
FB.api("/" + album_id + "/photos", function(response) {
callback(response);
});
}
By using this i am able to access all public data of logged in user, but not able to access data with limited permission(such as my location & Hometown is set to be visible to my friends only).
App dashboard permissions:
You can access fields such as hometown,location etc. for an user using the following code, which asks for an authorization when you login to your app using facebook.
FB.login(function(response) {
}, {
scope: 'user_location,user_hometown,user_photos,friends_location'
});
I need to pull the email addresses of all the people who are in my friendlist of facebook.
Is it possible to pull all the email address of my friends using javascript api.
It should be possible to any user, if they provide authentication.
You cannot access the email addresses of a user's friends. You can get their basic public information (user_id, display picture etc.).
To get the email address of these users, they would also have to authorise your application.
you can read the content using by sending a request to the graph api(http://developers.facebook.com/docs/reference/api/) in fb with a valid user access token ....
you can retrieve the user access token via javascript SDK
after getting that request the email of that particular user using fb.uimethods
sample code
FB.api('/', 'POST', {
batch: [
{ method: 'GET', relative_url: 'me'},
...
]
}, function (response) {
console.log(response);
});
the request url shuld be something like "https://graph.facebook.com/me?access_token=user_acess_token" & this will display the basic details about the user ...