Facebook user data request through the new social structure iOS6

I am trying to request user information using the new iOS 6 integration API. This is the code I use that is basically identical to what they demonstrated at WWDC:

{ NSDictionary *parameters = @{}; NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:parameters]; request.account = self.account; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"Facebook request received, status code %d", urlResponse.statusCode); NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"Response data: %@", response); dispatch_async(dispatch_get_main_queue(), ^{ }); }]; } 

The problem is that I get the error code 2500 from Facebook: "An active access token should be used to request information about the current user." If I changed the request to https://graph.facebook.com/[facebook id] then it works fine. I assume the problem is that iOS transfers the application access token instead of the user access token when sending a request through requestForServiceType. I just don’t know how to fix it. Obviously, anticipating and hard-coding my Facebook ids is not an option. Any suggestions?

+8
facebook ios6
source share
6 answers

add an active access token in the parameter, for example

 NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"PUT_ACCESS_TOKEN_HERE" forKey:@"access_token"]; 
+5
source share

I ran into the same problem and found a workaround:

 NSString *uid = [NSString stringWithFormat:@"%@", [[self.account valueForKey:@"properties"] valueForKey:@"uid"]] ; NSURL *url = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:uid]]; 
+2
source share

OK, this is how I do this integration with iOS 6 and get what I want from Facebook:

In AppDelegate, I do this:

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [FBSession.activeSession handleOpenURL:url]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [FBSession.activeSession handleDidBecomeActive]; } - (void)applicationWillTerminate:(UIApplication *)application { [FBSession.activeSession close]; } 

and in my ViewController, where I want to get information about myself or my friends, I do this (NOTE: this is a test, so you have many permissions!):

 NSArray *permissions = [NSArray arrayWithObjects:@"email", @"user_location", @"user_birthday", @"user_likes", @"user_interests",@"friends_interests",@"friends_birthday",@"friends_location",@"friends_hometown",@"friends_photos",@"friends_status", @"friends_about_me", @"friends_birthday", @"friends_hometown", @"friends_interests", @"friends_likes", @"friends_location", nil]; [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { /* handle success + failure in block */ if (status) { NSLog(@"Facebook Read Permission is successful!"); [self presentPostOptions]; // [self presentPostOptions]; } }]; 

Then in "presentPostOptions" I do this (in this example, I'm trying to extract something from my friend):

 - (void)presentPostOptions { [[FBRequest requestForMyFriends] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (!error) { NSArray *data = [user objectForKey:@"data"]; NSLog(@"%d", [data count]); for (FBGraphObject<FBGraphUser> *friend in data) { NSLog(@"%@", [friend first_name]); NSLog(@"%@", [friend last_name]); NSLog(@"%@", [friend id]); //make sure you have FBProfilePictureView outlet in your view //otherwise skip the profile picture! self.fbProfilePic.profileID = @"you'r friend profile.id"; } } else { NSLog(@"error"); // [self didFailWithError:error]; } }]; 

I don’t know what else you want to do, because in your question you were just trying to establish a connection, but in this way you can do whatever you want while you are integrated into iOS 6.

One more thing, make sure that you have the application settings via Facebook and the settings there, how to enable the application for iOS and ID for iPhone / iPad. Also FacebookAppID in your plist.

Let me know if this works for you,

EDIT: btw my Facebook SDK - 3.1.1.

+1
source share

I had the same error message, I fixed it by saving my account after updating the credentials.

0
source share

Make sure your (ACAccount *) facebookAccount (or, in your case, self.account) object is strong and you set it correctly when getting permissions.

0
source share

The answer is simple

in viewDidLoad () use:

 accountStore= [[ACAccountStore alloc]init]; facebookAccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *options= @{ ACFacebookAudienceKey: ACFacebookAudienceEveryone, ACFacebookAppIdKey: @"<YOUR FACEBOOK APP ID>", ACFacebookPermissionsKey: @[@"public_profile"] }; [accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) { if (granted) { NSLog(@"Permission has been granted to the app"); NSArray *accounts= [accountStore accountsWithAccountType:facebookAccountType]; facebookAccount= [accounts firstObject]; [self performSelectorOnMainThread:@selector(facebookProfile) withObject:nil waitUntilDone:NO]; } else { NSLog(@"Permission denied to the app"); } }]; 

And the function is (void) facebookProfile

 NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

Please note that the parameters you need are added as a dictionary. See the full list below https://developers.facebook.com/docs/graph-api/reference/user

 NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name",@"fields", nil]; SLRequest *profileInfoRequest= [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:param]; profileInfoRequest.account= facebookAccount; [profileInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"Facebook status code is : %ld", (long)[urlResponse statusCode]); if ([urlResponse statusCode]==200) { NSDictionary *dictionaryData= [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; } else { } }]; } 
0
source share

All Articles