How to retrieve the email id from facebook users - javascript

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 ...

Related

Get facebook email address from login

I'm currently using hello.js to have users create accounts and sign in with facebook. However, I need to use the user's facebook email address since this is used to send the user a confirmation of purchase.
Because the user can choose to not allow us to have his/her email address, I am left with an empty slot, thus the user never receiving confirmation of orders or downloadable files purchased through the system.
I've tried using the facebook user ID, but the email format is username#facebook.com. I can retrieve the user's ID and email (if allowed), but not the username.
Can we retrieve this and if so, how ?
I scratched my head too on how to fetch emails from FB, cause the response object in the documentation [https://developers.facebook.com/docs/facebook-login/web] paragraph Make API calls looks different from what it is returned.
You may want to look at:
[https://developers.facebook.com/docs/javascript/reference/FB.api]
and add parameters.
Here using version 2.5:
window.fbAsyncInit = function() {
FB.init({
appId : {your-app-id},
status : true,
cookie : true,
xfbml : true,
oauth : true,
version : 'v2.5'
});
Following the documentation, once logged:
FB.api('/me', function(response) {console.log(response) })
response has only _ID and name despite permissions are granted.
You can retrieve the email, if existing, by also specifying fields parameters against FB.api:
FB.api('/me?fields=id,name,email', function(response) {
console.log(response);
});

How do I use the access token to get email address and some basic info of facebook users

I have the follwing code and would like to take the email address and other details of facebook user. This is the code that I have done.
FB.login(function(response) {
if (response.authResponse) {
//updateUserInfo(fb_id);
console.log(response.authResponse);
updateUserInfo(response.authResponse.userID);
$.ajax({
dataType: "json",
url: 'http://graph.facebook.com/'+response.authResponse.userID+'/ fields=id,name,birthday,email,gender,hometown,location',
success: function(data){
fb_data = data;
console.log(data);
}});
},{scope:'email'});
I am unable to get get the following details:
birthday
email
Location
hometown
However, I am able to get the first name and id.
If I am supposed to use the access token, how am i supposed to put it in the query? The email address is the vital information as my system identifies user by email address.
You need following permission from user to get the data from Facebook.
user_birthday for birthday
email for email
user_location for Location
user_hometown for hometown
You can check this or this for other permissions and uses.
Then you will get the access token once user click on your Login button and authorize your App.
You dont have to use the ajax call after FB.login, you can use FB.api doc , which will ensure that the access token that you have obtained while logging in (requiring email access) will be used for accessing the data

FB Graph API: Posting as a page, to a different page

I'm using Facebook's Javascript SDK, and I need a way to let users post to a specific page impersonating one of the pages they own. Posting as the user works perfectly, but I can seem to get this right:
var data = {
access_token: access,
message: "Hello"
};
FB.api("/" + page_id + "/feed", "post", data, function(response) {
console.log(response);
});
page_id is the numeric ID of the specific page I want to post to, and access is the access_token retrieved from the "me/accounts" api, using the manage_pages authorization.
The output I recieve from the response variable is the following object:
error: Object
code: 200
message: "(#200) Posts where the actor is a page cannot also include a target_id"
type: "OAuthException"
I couldn't find the answer to this anywhere. Is this just not possible? It can't be.
quick googling suggests it's not possible:
https://stackoverflow.com/questions/9706873/post-on-wall-fan-page-to-a-fan-page-that-likes
How to Post with Application Name
also, take a look at the old REST API documentation: https://developers.facebook.com/docs/reference/rest/stream.publish/
target_id
Note: If you specify a Page ID as the uid, you cannot specify a
target_id. Pages cannot write on other users' Walls.
Note: You cannot publish to an application profile page's Wall.

Can an access token returned by Facebook to the Javascript SDK work server-side with the PHP SDK?

I'm building a website that makes use of Facebook connect. I'm authenticating users client-side with the javascript SDK and calling an AJAX method on my server every time a user logs in to check if the user is known to my app, and if the user is new to store their FBID in my database to register them as a new user.
My question is: Can the access token returned by Facebook to the Javascript SDK be used server-side (with the PHP SDK for example)? Can I send the access token string to the server via an AJAX call, store it in my database (along with a timestamp so I know how long it's valid for) and then use it to make calls to the graph API server-side? Is this even a logical thing to do?
Yes, this should work. Look at this question: How to properly handle session and access token with Facebook PHP SDK 3.0?
This is a workaround for the old JS and new PHP SDK. In my app I send the access token generated by the JS SDK via a form to my PHP. I have no doubts that this also works by sending the access token via ajax!
Using Jquery:
//Set an error message
var oops = ("Put your something went wrong message here.");
//Function to post the data to the server
function save(uid, accessToken){
$.post("../foo/bar", { uid: uid, access_token: accessToken, etc, etc }, function(data){
alert("Successfully connected to Facebook.");
location.reload();
}, "text");
}
function handler(x){
if (x.authResponse){
var token = x.authResponse.accessToken;
var uid = x.authResponse.id;
FB.api("/me/accounts", {access_token: token},
function(response){
if(response.data.length == 0) {
//Regular facebook user with one account (profile)
save(uid, token);
}else{
//Handle multiple accounts (if you want access to pages, groups, etc)
}
});
}else{
alert(oops);
}
}
FB.login(handler, {scope: 'The list of permissions you are requesting goes here'});
Any improvement suggestions are always appreciated.

facebook javascript api get user email

I am trying to fetch facebook user email for my app. But when i query the email address is returned as null.
Forums suggest that the user should grant the email permission to your app. How to make user grant that permission or ask him what fields will he give access to the application.
I am able to get name but not email from the code below.
FB.api(
{
method: 'fql.query',
query: 'SELECT name, email FROM user WHERE uid='+FB.getSession().uid
},
function(response) {
alert(response[0].name);
}
);
its prettry simple my dear just take permission at the time of login.
<fb:login-button perms="email" autologoutlink="true"></fb:login-button>
and take other permissions in perms with comma seperated values. I hope it will help

Categories