Logged in user's email address - facebook

I'm unable to fetch the user's email address even though I've added the email in the extended permissions for the app.
This is the preview box that I get when i'm editing the app's details
Yet i'm unable to get the email address of the logged in user.
This is the url i'm using for fetching the user info
https://graph.facebook.com/me?fields=first_name,last_name,email,gender&access_token=+accessToken
This is the data i'm getting from facebook
{
"first_name": "Varun",
"last_name": "Achar",
"gender": "male",
"id": "1XXXXXX66660851"
}
Any ideas?

I don't know why the Authenticated Referrals approach is not working for you, but here's how you can ask for the email address and get it in 2 other ways (you'll need to deactivate the authenticated referrals though):
(1) Serveri-Side Authentication - When facebook load your canvas page it's doing that by POSTing to your canvas url, providing the *signed_request* parameter. With that you can check if the user is authenticated or not (if he is, you get the access token and other things about the user such as his fb user id).
If the user is not authenticated you send him to the following url:
https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_url=YOUR_REDIRECT_URI&scope=email,publish_stream,etc
When the user gets back you should have an authenticated signed request.
(2) Client-Side Authentication - You use the javascript sdk to authenticate the user in your canvas page.
Once you have an access token you can get the email address by issuing a request to:
https://graph.facebook.com/me?fields=email
Or from the client side you can use:
FB.api("me?fields=email", function(response) {
console.log(response);
});
Edit
You should read the javascript sdk documentation? especially the FB.login part.
It shows an example of how to use it and asking for more permissions (using the the scope parameter)

This is how I finally got it working. Couldn't get it working through the Authentication Referrals so had to do this
FB.login(function(resp)
{
// Handle the response.
}
, {scope: "email, user_relationships"});

Related

Figure out which pages I got permissions for from the Facebook Login popup

I'm building a Chatbot and im trying to connect it with the client Facebook Fanpage programmatically, so I can you receive and send messages on the user behalf on the Fanpage.
So far all is good, I got the Facebook login to work, requesting access to the right permissions, and getting the user short and long lived access token.
However, I'm not sure this access token is valid for which Fanpages, The Facebook login lists all the user Fanpages and he can select which he will give the permission to per the screenshot, but nothing is returned in the payload saying which pages he selected, only the access token as below:
"authResponse": {
"accessToken": "EAAJ911mLyc4BAPsNTMCfVqcf5JkvnGsaZCxXZAZA6slQuubSaZANMB3jsUBZB5ds7opfAuV1ZBB5Vz3NZBLwiN3QO1C5NupvGAbIdzxz91JYWHZCJfwOgawzOswgxAevLDmd6yjRQ4LASROcbnyTtHcnBeE08qvWSYJ6sJrzZCptZB94NZA1tz6tVKOIZCIgrhdaM1IB4feF2LCaF86eIXH1yAZCE",
"userID": "2917511921114302",
"expiresIn": 5498,
"signedRequest": "YpjihSoIBgcl2fUxkVBE2b7_mtXD7RK7ZT7sn77uqRY.eyJ1c2VyX2lkIjoiMjkxNzUxODkyMTcxNDMwMiIsImNvZGUiOiJBUUFhNGVZRlJTUnIyNDkwT252dFdEYTlfTnh3cm1QWnNoTUF4bzNUYzdERS00MmZXT0pvTEY4XzUybUJ6VmhzeF81eG05VjFNZV33daVhvM3lQV1FscTE1czZoM25uYUpnTzdObGpxUkJhQTNpRnhtMTA1TFMtbVRPa0dTNGNiNEFpbXN3dTcxcEZiX0hjZG1MVXFldmo5QkVQMXJzYnRkYkw5LWNzSXZlWWIyWmZQR25TNW93aUdCTjRIZmRqdjRSbm9aMkllRDRPN3R1RUtfbmhDMG55cDd1UDZGR1JCOVFaV3ZQQUVEQTNwN2dJX0x3X1ZDYUsteG00Tk9BbkJhZEdnVS16eXJNR3J1S0owZWFLUFF6YkFkNWJRaUJzNmw1a2xvUl9IOFhieFpiMjlaUmJHTjBOUjJoZmk4bHNISU5yMW91SjUyXzJqWTNaTy1xZ3FSX1RObSIsImFsZ29yaXRobSI6IkhNQUMtU0hBMjU2IiwiaXNzdWVkX2F0IjoxNjc1OTUyOTAyfQ",
"graphDomain": "facebook",
"data_access_expiration_time": 1683728902
},
"status": "connected"
}
And screenshots for the page selection from the Facebook login:
How can I can get the pages id selected, so I can request later the page access token for those specific pages? Thanks.

Get EmailId from UID?

I got the following from fiddler whenever I try to login to dropbox.
[{"uid": 663175210, "sess_id": 272608865417613682242215474540606531660, "expires": 1495277184, "team_id": 2059286, "role": "work"}]
Notice that uid is being passed. I did some google and found that the uid is The Dropbox user ID of the authorized user.
Is there any way i can get the user email id (used to login to dropbox) using dropbox API, given that i have the corresponding uid?
No, the Dropbox API doesn't offer a way to retrieve the user's email address by supplying the uid.
If the user has authorized your app on the API though, you can use the /2/users/get_current_account API call to get their account information, including email address.

Making it mendatory to get email with facebook oAuth with Socialite

I am trying to use facebook oAuth for my users to login/register to my website. For this, i require user's email address
Facebook oAuth dialog has option to provide / not provide email but even if the user's doesn't provide their email, I cannot re-request authorization from facebook again as it considers the user as logged in and simply redirects to call back url.
I am using Laravel and Socialite package.
Any solutions??
Socialite allows you to get user information directly after the successful authorization by using
$user = Socialite::driver('facebook')->user();
$user->email;
I would expect $user->email to contain the users email address if he accepted to share her with the app and to be empty if the user denied the email address sharing.
And if $user->email is empty throw an error and ask the user to re-authorize. If it's not working you could try to de-authroize the user, a guide can be found here.
A convenient way to avoid any issues is to display a warning that the user should allow email sharing if he wants to log in with Facebook.

How can I identify Facebook users that don't use my app yet?

I'm developing an app in which users log with their Facebook account, but only the Facebook profiles that the system administrator have previously entered.
In other words, the system admin enters a bunch of Facebook profiles, and when the user tries to log into my app, the system checks if there's a record for that Facebook profile.
I used to do this using the user's Facebook username, because the admin could get that information easily from a Facebook profile's URL, and Facebook provided that username when the user logged in. This is not possible anymore with Graph API 2.0, because Facebook does not provide the username field anymore.
So do you guys have any suggestion on how the system administrator could easily identify users, so they would be granted access in my app?
#WizKid is right, using the user's email is the best way to do this. Any user will be able to login, but then you can build in a check to see if that user's email is in your "pre-approved" list.
To get the user's email address, you need to ask them for it in the login dialog using the scope parameter. In the JS SDK, it looks like this:
FB.login(function (response) {
// handle response
}, { scope: "email" }); // <<< the important bit
Note: with the new Graph 2.0 Login dialog, a user can choose not to provide their email address by unchecking it in the "edit the info you provide" section of the dialog. As a result you'll need to double check that the user actually provided their email address.

Facebook: App Access token, Posting to a User

I went through this wonderfull tutorial on Using App Access token. According to this post it says that:
If the user has not provided the appropriate permissions to publish on the user’s behalf, you will receive an error message. For example, if the user has not provided the publish_stream permission, the following error will be returned:
{
"error": {
"message": "(#200) The user has not granted the application the permission to automatically publish feed stories",
"type": "OAuthException",
"code": 200
}
}
Now is there any other way to post to a users wall who has not authorized the app?
My query is this:
I have two logins for my app, one Login with Email and other FB Connection. So a user may login with email or facebook to my app. If he logs in with facebook there is no problem at all as i have his access token. If he login with email , then i don't have his user access token, so in this case i want to use the App Access token to fetch the users friends and then allow him to post to friends wall [friends who are not a member of our app / not authorized our app].
there is no way to post to a user wall who has not authorized the app.
one possible soln:
you have to link up the user login via the email and fbID
stored the access token when the user accesses your app via fbID
if the user login via the email, then get the access token from local storage and use it for the posting.