Unable to get Email Id Of logged person in Facebook ios SDK - ios

I am working on a Facebook IOS app to fetch user data(Public Profile,Email) from the Facebook.After implementing login process i am able to fetch first name,last name and url for profile picture of the User but the email i am getting is null. i have passed the permission for getting public Profile and Emial.but i am not getting Email Of the person.
I am using facebook SDK for ios V4.0.1
Here is my code
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me"
parameters:#{#"fields": #"first_name, last_name, picture, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *firstName = [NSString stringWithFormat:#"%#",[result objectForKey:#"first_name"]];
NSString *lastName = [NSString stringWithFormat:#"%#",[result objectForKey:#"last_name"]];
NSString *email = [NSString stringWithFormat:#"%#",[result objectForKey:#"email"]];
NSString *picurl = [NSString stringWithFormat:#"%#",[result objectForKey:#"picture"]];
NSLog(#"email is %#", [result objectForKey:#"email"]);
NSLog(#"picture is %#", [result objectForKey:#"picture"]);
}
else{
NSLog(#"%#", [error localizedDescription]);
}
}];
I am getting the Following Output:
email is (null)
picture is {
data = {
"is_silhouette" = 0;
url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/p50x50 **************_************_n.jpg?oh=ed5fd60c770cb5f34424b3fcfb4ca174&oe=559F472D&__gda__=1437970763_989a264cf7ec16f5d51b0a58b7e7f609";
};
}
why i am not getting Email Address of the User

While logging. You must ask user permission for email or else you would get email as (null).
Would not fetch Email ID
FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init];
loginView.readPermissions = #[#"public_profile"];
IF you login with above permission then you would not be able to get email id it will always be return (null).
Would fetch Email ID with Public profile
FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init];
loginView.readPermissions = #[#"email"];
Our app gets public_profile permission automatically upon any successful login but it does not get email.
Same goes for custom login as well.
Refer to :https://developers.facebook.com/docs/facebook-login/ios/v2.3#login-apicalls

If you were to assume the logic that setting FBSDKLoginButton.readPermissions to ["public_profile","email"] would be the way to express that you want the Facebook user's "public_profile" and their "email" address you will be sorely mistaken. Very confusing in my opinion, the following gives you both the Facebook user's public profile and their email address:
FBSDKLoginButton.readPermissions = ["email"]

Related

Why iOS Facebook Signup Email id not returned in my account?

I'm using signup with Facebook. Other Facebook login works fine. Some accounts are working fine, but some accounts are not returning email. If anyone knows the reason please help me!
- (void)facebookOpenSession{
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_birthday",#"basic_info",#"user_location",#"user_likes",#"email",#"public_profile",#"user_hometown",#"user_about_me",nil];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: permissions
fromViewController:self.view.window.rootViewController
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {
} else if (result.isCancelled) {
} else {
if ([result.grantedPermissions containsObject:#"email"])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me"
parameters:#{#"fields": #"picture, email,name,first_name,last_name"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
if ([result objectForKey:#"email"]!=NULL)
{
[self newFbLoginFunction:result];
}
}
}];
}
}
}];}
Response like this:
{
"first_name" = AAAA;
id = 1XXXXXXXXX2258;
"last_name" = BBBBB;
name = "AAAA BBBBB";
picture = {
data = {
"is_silhouette" = 0;
url = "https://fb-s-c-a.akamaihd.net/h-ak-xpa1/v/t1.0-1/p50x50/XXXXXXXXXXX3212_3395344935279367518_n.jpg?oh=7345e4de6XXXXXXXXXXXX8&oe=58E16A60&__gda__=14951668XXXXXXXXXX2bc0aec58eab3f25e47";
};
};}
Not all users use an email to login, you can also use a phone number. Also, emails need to be approved by Facebook or they will not show up with the API.
In other words: You can never guarantee to get an email back. Present an email input field to the user if you can´t get the email with the API.

Unable to get friends list on Facebook SDK 4 for iOS

I am trying to get list of friends using Facebook SDK with no luck.
This is the code I use:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/friends"
parameters:#{#"fields": #"fields=id, first_name, last_name, birthday, gender, picture, devices"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (error == nil) {
NSArray* friends = [result objectForKey:#"data"];
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
This is the error I get:
2016-05-08 00:07:12.610 App2Gift[48103:676830] Failed getting friend list
2016-05-08 00:07:12.610 App2Gift[48103:676830] Error: Error Domain=com.facebook.sdk.core Code=8 "(null)" UserInfo={com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=100, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={
body = {
error = {
code = 100;
"fbtrace_id" = "G3OpyiU/JKs";
message = "(#100) Unknown fields: fields=id.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=400, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=(#100) Unknown fields: fields=id., com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0} {
"com.facebook.sdk:FBSDKErrorDeveloperMessageKey" = "(#100) Unknown fields: fields=id.";
"com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey" = 0;
"com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode" = 100;
"com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey" = 400;
"com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey" = {
body = {
error = {
code = 100;
"fbtrace_id" = "G3OpyiU/JKs";
message = "(#100) Unknown fields: fields=id.";
type = OAuthException;
};
};
code = 400;
};
}
Running the same with parameters as: nil works but doesn't give back all the needed data.
Tested and working, with these permissions:
loginView.readPermissions = #[#"public_profile", #"email", #"user_friends"];
NSString *path = [NSString stringWithFormat:#"/me/friends"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:path
parameters:#{#"fields": #"id, name, picture, devices"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
}];
In v2.0 of the Graph API, calling /me/friends returns the person's friends who also use the app.
In addition, in v2.0, you must request the user_friends permission from each user. user_friends is no longer included by default in every login. Each user must grant the user_friends permission in order to appear in the response to /me/friends. See the Facebook upgrade guide for more detailed information

iOS Facebook graph request error when looking up {user-id} path

I'm using test users with the IOS Facebook SDK. I create test users and retrieve one userID. Then I create a graphPath string that resembles the "/me" path: /{user-id}
NSString* userId = [[self.testUserTokens objectAtIndex:0] userID];
NSString* graphPath = [NSString stringWithFormat:#"/%#", userId];
Then I make the graph request.
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:#{#"fields": #"name, id"}];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:request
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"data = %#", result);
if (error)
NSLog(#"error = %#", error);
//TODO: process me information
}];
[connection start];
This returns a 400 "Bad Request" error. I don't understand why at all, I thought I was following the correct format from the Facebook documentation. Does anyone know what I'm doing wrong? I figure it's just a small mistake I can't notice.
Edit: After using the Facebook Graph Explorer, it appears that a /{user-id} request works correctly, using my own Facebook User ID for instance. But it does not work with the IDs of my test users, which I can retrieve either in the Roles panel of Facebook Developers Dashboard or in my app using the method above. For my own ID, I notice that it is different whether I'm retrieving it from the Graph Explorer or from my app, which is logical considering Facebook explains IDs differ based on the app being used, I believe.
Doing the request above using my own personal ID, retrieved using [FBSDKAccessToken currentAccessToken].userId works perfectly fine. So the issue seems to stem from the test user IDs.
Facebook Developer support has resolved this issue for me after I submitted it as a bug.
Turns out it was required to pass a user access token, which I assume is required when wanting to access direct {user-id} data instead of "me" data.
The code thus looks like this:
[testManager requestTestAccountTokensWithArraysOfPermissions: #[[NSSet setWithObjects:#"email", #"user_friends", nil], [NSSet setWithObjects:#"email", #"user_friends", nil]] createIfNotFound:NO completionHandler:^(NSArray* tokens, NSError* error)
{
self.testUserTokens = tokens;
NSLog(#"token1 = %#", [[self.testUserTokens objectAtIndex:0] tokenString]);
NSString* userId = [[self.testUserTokens objectAtIndex:0] userID];
NSString *accessToken = [[self.testUserTokens objectAtIndex:0] tokenString];
NSString* graphPath = [NSString stringWithFormat:#"/%#", userId];
NSLog(#"graphPath = %#", graphPath);
[[[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:#{#"fields": #"name, id, email"} tokenString:accessToken version:nil HTTPMethod:#"GET"] startWithCompletionHandler:^(FBSDKGraphRequestConnection* connection, id result, NSError* error)
{
NSLog(#"data = %#", result);
if (error)
NSLog(#"error = %#", error);
}];

How get list of friends and email using Facebook sdk

I have a problem i implement Facebook SDK in my view controller and want to get list of friends and email using Facebook. I wrote this:
FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions:#[#"public_profile", #"email", #"user_friends"]];
loginView.delegate = self;
and then use the protocol method of FBLoginViewDelegate:
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{
self.profilePictureView.profileID = user.id;
self.nameLabel.text = user.name;
NSLog(#"%#", user);
NSLog(#"%#", [user objectForKey:#"email"]);
}
In console i got this:
2014-12-04 14:56:48.746 FBLoginUIControlSample[2941:613]
"first_name" = Pavel;
gender = male;
id = 1379600000000000;
"last_name" = name;
link = "https://www.facebook.com/app_scoped_user_id/1379600000000000/";
locale = "ru_RU";
name = "Pavel name";
timezone = 2;
"updated_time" = "2014-12-03T11:47:13+0000";
verified = 1;
2014-12-04 14:56:48.748 FBLoginUIControlSample[2941:613] (null)
sorry , but you can't
Read Facebook Permissions
you only can get the email when the user login , but you can't get user's friends email.
i am not really sure but maybe you can use "friendsusername"#facebook.com ("facebook email").
i found this, https://www.facebook.com/help/224049364288051
the person has to active #facebook option.
For friends with Facebook SDK 3.0 you can do this
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
}
}];
You can not access to any users email, excepts of yours.
Because FBGraphUser doesn't have an email #property, we can't access the information like with the name (dot-syntax), however the NSDictionary still has the email kv-pair and we can access it like we would do with a normal NSDictionary.
With this method you can access to your email.
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
self.nameLabel.text = user.name;
self.emailLabel.text = [user objectForKey:#"email"];
}
}];
}
hope this will help you
you can get your friend list by using below code in your viewDidLoad
[FBRequestConnection startWithGraphPath:#"me/taggable_friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
}];
and some info
EDIT
the above answer only mention for friendlist not email(as its already quite clear by above answers) , now what you want friendlist
A friend list(Friend List V 2.2) - an object which refers to a grouping of friends created by someone or generated automatically for someone (such as the "Close Friends" or "Acquaintances" lists)
from v2.0 you'll only be able to get all friends via the /me/taggable_friends (see it by result)

OAuthException/ code = 2500/ message = "An active access token must be used to query information about the current user."

I am trying to get data (like name,gender,birthday, profile picture etc) of the user that is logged in. I have tried following two codes, shown below:
//first: Create request for user's Facebook data
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"%#", error);
if (!error) {
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
NSLog(#"%#....",userData);
_facebookID = userData[#"id"];
_name = userData[#"name"];
_location = userData[#"location"][#"name"];
_gender = userData[#"gender"];
_birthday = userData[#"birthday"];
NSLog(#"%#",_name);
_picutreURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1", _facebookID]];
self.nameField.text=_name;
self.profilePicture.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:_picutreURL]];
self.profileInfo.text=[NSString stringWithFormat:#"%#, Live in %# , Born on %#",_gender,_location,_birthday];
}
}];
// 2nd:Fetch user data
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user,
NSError *error) {
if (!error) {
NSString *userInfo = #"";
// Example: typed access (name)
// - no special permissions required
userInfo = [userInfo
stringByAppendingString:
[NSString stringWithFormat:#"Name: %#\n\n",
user.name]];
// Example: typed access, (birthday)
// - requires user_birthday permission
userInfo = [userInfo
stringByAppendingString:
[NSString stringWithFormat:#"Birthday: %#\n\n",
user.birthday]];
// Example: partially typed access, to location field,
// name key (location)
// - requires user_location permission
userInfo = [userInfo
stringByAppendingString:
[NSString stringWithFormat:#"Location: %#\n\n",
user.location[#"name"]]];
// Example: access via key (locale)
// - no special permissions required
userInfo = [userInfo
stringByAppendingString:
[NSString stringWithFormat:#"Locale: %#\n\n",
user[#"locale"]]];
// Example: access via key for array (languages)
// - requires user_likes permission
if (user[#"languages"]) {
NSArray *languages = user[#"languages"];
NSMutableArray *languageNames = [[NSMutableArray alloc] init];
for (int i = 0; i < [languages count]; i++) {
languageNames[i] = languages[i][#"name"];
}
userInfo = [userInfo
stringByAppendingString:
[NSString stringWithFormat:#"Languages: %#\n\n",
languageNames]];
}
// Display the user info
NSLog(#"%#",userInfo);
}
}];
But I get null string in output and detected this error:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x9499320 {com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}
What should I do?
I believe I had the same problem, and the way I just got it working was to add
FBSession.activeSession = fbSession;
once the initial handler came back successfully. (fbSession is whatever variable you have with the facebook session in it.) I just randomly stumbled upon this (I think from another StackOverflow post). But I don't see it in the docs, and by grepping, it doesn't seem to be in any of the Sample apps. So maybe there's a larger issue with my code that I'm not seeing? I'd be interested to hear how others avoid setting the activeSession, and still are able to run startForMeWithCompletionHandler.

Resources