FBRequest requestForMe没有回复

时间:2013-06-22 16:33:37

标签: ios facebook

我正在关注Facebook开发者网站上的示例代码,例如我无法获取我的名字。我正在使用此代码:

- (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);
             }
         }];
    }

}

所以当会话打开时,我看到了这个输出:

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}

在Facebook应用页面上,我已将AppId设置为0,因为它尚未上传。 BundleId设置正确。

我缺少什么或者还有其他方法吗?

1 个答案:

答案 0 :(得分:3)

您应该通过调用

将appDelegate.session设置为活动会话
[FBSession setActiveSession:appDelegate.session]; 

当你打开它时。

这是因为FBRequest的requestForMe方法使用活动会话(如文档中所述)。