2011-09-11 57 views
0

我被Facebook API难住登录了。我找不到任何有用的东西。Facebook OAuth api登录问题

我FBSessionDelegate方法不会被调用,并和的accessToken EXPIRATIONDATE值没有被设置,所以我不认为我会永远登录。

我恢复了一个非常简单的应用程序,只有两个按钮(登录,注销)和标签以查看状态信息。

下面是视图控制器,其中所有的处理发生的代码:

// 
#import <UIKit/UIKit.h> 
#import "Facebook.h" 

@interface facebook_login_testViewController : UIViewController <FBSessionDelegate>{ 

    UIButton *loginButton; 
    UIButton *logoutButton; 
    UILabel *info; 

    Facebook *facebook; 

} 

@property (nonatomic, retain) Facebook *facebook; 

- (void) loggingIn; 
- (void) loggingOut; 

@end 

#import "facebook_login_testViewController.h" 

@implementation facebook_login_testViewController 
@synthesize facebook; 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [loginButton setTitle: @"Log In" forState:UIControlStateNormal]; 
    [loginButton addTarget:self action:@selector(loggingIn) forControlEvents:UIControlEventTouchUpInside]; 
    loginButton.frame = CGRectMake(200, 200, 200, 50); 

    logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [logoutButton setTitle: @"Log Out" forState:UIControlStateNormal]; 
    [logoutButton addTarget:self action:@selector(loggingOut) forControlEvents:UIControlEventTouchUpInside]; 
    logoutButton.frame = CGRectMake(200, 300, 200, 50); 

    info = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 400, 600)]; 
    info.numberOfLines = 0; 

    [self.view addSubview:loginButton]; 
    [self.view addSubview:logoutButton]; 
    [self.view addSubview:info]; 

    info.text = @"Waiting to log in...\n\nPress the login button."; 

    facebook = [[Facebook alloc] initWithAppId:@"159...........5" andDelegate:self]; 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
     && [defaults objectForKey:@"FBExpirationDateKey"]) { 
     facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
     facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
    } 

} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload];   
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 

- (void) loggingIn{ 
    if (![facebook isSessionValid]) { 
     [facebook authorize:nil]; 
    } 
} 

- (void) loggingOut{ 
    info.text = [NSString stringWithFormat:@"\naccess token: %@\nexpiration date: %@\nfacebood description:%@\n",facebook.accessToken, facebook.expirationDate, facebook]; 
    [facebook logout:self]; 

} 

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 

    return [facebook handleOpenURL:url]; 
} 

- (void)fbDidLogin { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 

} 

@end 

的的.plist文件被设置为这样

enter image description here

这真的看起来太简单了,错了。当我点击登录按钮时,我可以跟踪authorize:permission,然后authorize:permissions localAppId:localAppId,并且就我可以追踪它而言,我没有看到任何看起来会导致isSessionValid成为TRUE的结果。

我等待,然后按注销按钮,看看Facebook的一些参数,在万一发生异步的需要,他们仍然是零。

有人能看到我缺少的东西吗?

回答

1

有上显示这是如何工作的净几个例子,但Facebook的SDK中分布有演示应用程序(DemoApp)一旦你在的appid插上的作品。 (如果您没有插入appId值,它将在检查该值时被杀死。)

授权过程中发生的是[facebook授权:权限]被调用。 (权限不应该是无)

这导致到呼叫[自我认证:权限localAppId:无],然后调用[自authorizeWithFBAppAuth:YES safariAuth:YES]。在该方法中,可以尝试连接到Facebook以进行授权的三种方式。

在每个这些异步方法中,应用程序委托是处理所得到的作为回调的一部分应用委托:(UIApplication的*)应用handleOpenURL:(NSURL *)URL。如果在其他在线教程中提到了这一点,很容易错过,因为它们通常会在应用程序委托中执行所有处理,从我所看到的情况来看。就我而言,我在主视图控制器中完成了主要处理。

的修复,于我而言,是移动应用:(UIApplication的*)应用程序handleOpenURL:(NSURL *)网址方法应用委派,并将其指向Facebook的对象是这样的:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    return [[controller facebook] handleOpenURL:url]; 
} 

之后,其他Facebook委托方法(fbDidLogin,fbDidLogout)被调用,我收到accessToken和expirationDate。所以看起来现在这个工作正在进行。