ghostrider
ghostrider

Reputation: 5259

FBRequest requestForMe does not respond

I am following the sample codes on Facebook developer's site and I cannot fetch my name for example. I am using this code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    if (!appDelegate.session.isOpen) {
        NSLog(@"Session is not open");
        // create a fresh session object
        appDelegate.session = [[FBSession alloc] init];

        // if we don't have a cached token, a call to open here would cause UX for login to
        // occur; we don't want that to happen unless the user clicks the login button, and so
        // we check here to make sure we have a token before calling open
        if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
            // even though we had a cached token, we need to login to make the session usable
            [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                // we recurse here, in order to update buttons and labels
            }];
        }
    } else {
        NSLog(@"Session is open");

        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
             if (!error) {
                 NSLog(@"logged in : %@, id: %@",user.name,user.id);
             } else {
                 NSLog(@"error: %@", error);
             }
         }];
    }

}

so when the session is open, I see this output:

Session is open
error: Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x1fd60620 {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}

On facebook app page, I have set AppId to 0 since it is not uploaded yet. BundleId is set correctly.

What am I missing or is there any other way to do so?

Upvotes: 0

Views: 1957

Answers (1)

Ming Li
Ming Li

Reputation: 15662

You should set your appDelegate.session as the active session by calling

[FBSession setActiveSession:appDelegate.session]; 

when you open it.

This is because FBRequest's requestForMe method uses the active session (as stated in the docs).

Upvotes: 3

Related Questions