Querying Facebook user data through new iOS6 social framework
Asked Answered
P

6

8

I am trying to query information about a user using iOS 6's new Facebook integration API. This is the code I'm using, which is basically identical to what they demoed 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(), ^{

    });
}];
}

Problem is I am getting error code 2500 from Facebook: "An active access token must be used to query information about the current user." If I change the query to https://graph.facebook.com/[facebook id] then it works fine. I am assuming the problem is that iOS is passing the access token of the app instead of the user's access token when sending the request via requestForServiceType. I just don't know how to fix it. Obviously anticipating and hardcoding the Facebook IDs of my users is not an option. Any suggestions?

Psychokinesis answered 30/6, 2012 at 1:27 Comment(1)
Did you get the answer??As iOS 6 is released.Cheney
D
5

add your active access token in paramete like

NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"PUT_ACCESS_TOKEN_HERE" forKey:@"access_token"];
Doggish answered 10/7, 2012 at 7:24 Comment(1)
How do you get that access key in code? Or are you proposing putting an actual access code in there? Seems like access codes expire so that wouldn't be a good option.Cardie
S
2

I faced with the same issue and found the workaround:

NSString *uid = [NSString stringWithFormat:@"%@", [[self.account valueForKey:@"properties"] valueForKey:@"uid"]] ;                
NSURL *url = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:uid]];
Selfexcited answered 20/9, 2012 at 13:10 Comment(1)
The issue was in incorrect app settings on FB. I changed settings according to this post #12644729 and it resolved the issue.Selfexcited
G
1

OK here 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 retrieve information about myself or my friends I do this (NOTE: this is a test, so it is a lot of 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 try to retrieve 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's profile.id";

             }
         }
         else
         {
             NSLog(@"error");
             //  [self didFailWithError:error];
         }
     }];

I don't know what else you want to do because in your question you just tried to make a connection but this way you can do what ever you want while you are integrated in iOS 6.

One more thing, make sure about your App Settings over Facebook and the configs over there like enable your app for iOS and the ID for iPhone/iPad. Also the FacebookAppID in your plist.

Let me know if it works out for you,

EDIT: btw my Facebook SDK is 3.1.1.

Guayule answered 4/1, 2013 at 5:56 Comment(0)
W
0

I had the same error message, I fixed it by saving my account after renewing the credential.

Workout answered 14/9, 2012 at 11:47 Comment(0)
D
0

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

Dibrin answered 17/10, 2012 at 9:54 Comment(0)
F
0

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-(void)facebookProfile

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

Notice the params you need are added as dictionary Refer below for complete list 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 {

    }
}];


}
Feu answered 11/7, 2015 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.