我正在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设置正确。

我想念的是什么?还有其他方法可以做到吗?

最佳答案

您应该通过调用将appDelegate.session设置为活动会话

[FBSession setActiveSession:appDelegate.session];


当您打开它时。

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

关于ios - FBRequest requestForMe没有响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17252903/

10-11 07:52