Facebook Graph GET Request - ios

My app is recieving a error when making a GET Request to the Facebook API for the endpoint me.
Here is my code:
-(void) updateUserInformation{
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"/me:" parameters:#{#"fields": #"id, name, email, user_birthday"}];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(!error){
NSDictionary *userDictionary = (NSDictionary *)result;
NSMutableDictionary *userProfile = [[NSMutableDictionary alloc] initWithCapacity:8];
if (userDictionary[#"name"]) {
userProfile[#"name"] = userDictionary[#"name"];
}
if (userDictionary[#"first_name"]) {
userProfile[#"first_name"] = userDictionary[#"first_name"];
}
if (userDictionary[#"location"][#"name"]) {
userProfile[#"location"] = userDictionary[#"location"][#"name"];
}
if (userDictionary[#"gender"]) {
userProfile[#"gender"] = userDictionary[#"gender"];
}
if (userDictionary[#"birthday"]) {
userProfile[#"birthday"] = userDictionary[#"birthday"];
}
if (userDictionary[#"interested_in"]) {
userProfile[#"interested_in"] = userDictionary[#"interested_in"];
}
[[PFUser currentUser] setObject:userProfile forKey:#"profile"];
[[PFUser currentUser] saveInBackground];
}else {
NSLog(#"Error in Facebook Request %#", error);
}
}];
}
There error that I am getting is:
2015-10-11 14:30:59.306 MatchedUp[2358:139193] Error in Facebook Request Error Domain=com.facebook.sdk.core Code=8 "(null)" UserInfo={com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=803, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={
body = {
error = {
code = 803;
"fbtrace_id" = "CfGbwnx4/B9";
message = "(#803) Some of the aliases you requested do not exist: me:";
type = OAuthException;
};
};
code = 404;
}, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=404, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=(#803) Some of the aliases you requested do not exist: me:, com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0}
I am not very sure what this error means. It says "Some of the aliases you requested do not exist: me:" does that mean the result dictionary in the completionHandler does not contain some of the information for the endpoint me? Any help would be appreciated. Thank you!

Try changing your graph path #"/me:" to just #"me".
Good luck!

Related

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

FBSDK startForMyFriendsWithCompletionHandler giving error 400

I am trying to retrieve list of FB friends which use the app. It was working fine until last month. I just realized that it is not working any longer. I am using the following block to achieve the results:
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
}
else
{
NSLog(#"%#",error)
}
}];
But quite contrary to previous results, Now I get the following error:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x174278280 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ErrorSessionKey=<PFReceptionist: 0x174026dc0>, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Unknown fields: username.";
type = OAuthException;
};
};
code = 400;
}}
Has the facebook sdk changed lately? It might be so because I can't get the gender and date of birth too now, which I was able to retrieve before. What do you reckon is the issue? How can I retrieve the friends who are using my app, as it is quite crucial for my app?
I fixed it using this block instead.
FBRequest *friendsRequest = [FBRequest requestForGraphPath:#"/me/friends"];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
if(error)
{
NSLog(#"%#", error);
}
else
{
NSLog(#"%#", result);
}
}];

CloudKit: notificationChangedBlock no response

I working in app using CloudKit. I'm implement a way to get deltas from CloudKit to my app (I'm using the public database). I get a response back from fetchNotificationChangesCompletionBlock block but I don't get a response back from notificationChangedBlock.
Here is my code:
-(void)checkForNewData
{
CKServerChangeToken *token = [[cloudKitToken helper] getCloudKitToken];
CKFetchNotificationChangesOperation *op = [[CKFetchNotificationChangesOperation alloc]
initWithPreviousServerChangeToken:token];
NSMutableArray *noteIds = [[NSMutableArray alloc] init];
op.notificationChangedBlock = ^(CKNotification *note)
{
CKNotificationID *noteId = note.notificationID;
[noteIds addObject:noteId];
if([note isKindOfClass:[CKQueryNotification class]]) {
CKQueryNotification *qNote = (CKQueryNotification *)note;
CKRecordID *changedRecordID = qNote.recordID;
NSLog(#"I got CKQueryNotification");
}
};
op.fetchNotificationChangesCompletionBlock = ^(CKServerChangeToken *token, NSError *error) {
NSLog(#"error %#", error.localizedDescription);
};
[[CKContainer defaultContainer] addOperation:op];
}
Any of you knows what I'm doing wrong or what is wrong with my code?
I'll really appreciate your help.

Parse.com can't read facebook user details when app restart

I'm using Parse Version 1.2.19 and I can't access a facebook user's name.
if ([PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
// Create Facebook Request for user's details
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *displayName = result[#"name"];
if (displayName) {
self.title =[NSString stringWithFormat:NSLocalizedString(#"Welcome %#!", nil), displayName];
}
}else{
NSLog(#"%#",[error description]);
}
}];
}
I get the following error:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x29e24040 {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}
These 2 posts claim the issue is fixed in the latest parse version:
https://www.parse.com/questions/error-when-trying-to-reauthorise-facebook-user
https://www.parse.com/questions/oauthexception-code-2500-an-active-access-token-must-be-used
It seems I needed this:
[PFFacebookUtils initializeFacebook];
Before calling the request. That line of code wasn't being called when the app just reloaded.

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