2011-09-11 54 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。所以看起來現在這個工作正在進行。