2011-10-18 203 views
1

我知道這個問題已被問到,但我一直在通過線程閱讀,並應用了所有我能找到的修復程序,並且仍然存在問題。Facebook SDK + iPhone SDK + SSO

我想實現Facebook SDK的新SSO功能,以允許我的應用程序使用Facebook的應用程序而不是webView進行身份驗證。當我的應用切換到Facebook的應用時,它表示加載幾秒鐘並切換回來,但令牌仍爲空。我有一種感覺它與我的應用程序的URL方案做的,但我已經跟着很多教程和我相當有信心,我目前的plist值是正確的: enter image description here

這裏是代碼我在我的appDelegate實現,的viewController文件: appDelegate.h

Facebook *facebook; 

appDelegate.m

// Pre 4.2 support 
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    return [facebook handleOpenURL:url]; 
} 

// For 4.2+ support 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 
    return [facebook handleOpenURL:url]; 
} 

SecondViewController.m

-(IBAction) fbButton { 
    //FACEBOOK INTEGRATION 
    appDelegate.facebook = [[Facebook alloc] initWithAppId:@"222087841143437" andDelegate:appDelegate]; 

    // Check and retrieve authorization information 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
     && [defaults objectForKey:@"FBExpirationDateKey"]) { 
     appDelegate.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
     appDelegate.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
    } 
    if (![appDelegate.facebook isSessionValid]) { 
     appDelegate.facebook.sessionDelegate = self; 
     [appDelegate.facebook authorize:permissions]; 
     NSLog(@"Token: %@", [defaults objectForKey:@"FBAccessTokenKey"]); 
    } else { 
     NSLog(@"Already logged in!"); 

    } 
    [appDelegate.facebook requestWithGraphPath:@"me" andDelegate:self]; 

} 


- (void)request:(FBRequest *)request didLoad:(id)result { 
    NSString *nameID = [[NSString alloc] initWithFormat:@"%@ (%@)", [result objectForKey:@"name"], [result objectForKey:@"id"]]; 
    NSMutableArray *userData = [[NSMutableArray alloc] initWithObjects: 
           [NSDictionary dictionaryWithObjectsAndKeys: 
           [result objectForKey:@"id"], @"id", 
           nameID, @"name", 
           [result objectForKey:@"picture"], @"details", 
           nil], nil]; 

    [userData release]; 
    [nameID release]; 
    NSLog(@"DATA RECEIVED: %@", result); 
} 

- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"request:didReceiveResponse:"); 
} 

- (void)request:(FBRequest *)request didFailWithError:(NSError *)error { 
    NSLog(@"%@", [error localizedDescription]); 
    NSLog(@"Err details: %@", [error description]); 
}; 
- (void)fbDidLogin { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[[appDelegate facebook] accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[[appDelegate facebook] expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
    NSLog(@"Facebook Logged in!"); 

    [appDelegate.facebook requestWithGraphPath:@"me" andDelegate:self]; 


} 

我知道我調用了[appDelegate.facebook requestWithGraphPath:@「me」andDelegate:self]兩次,但我不認爲這是問題所在。

下面是從控制檯的一個片段,當我按下登錄按鈕:

2011-10-18 15:37:33.287 Track[2235:707] Token: (null) 
2011-10-18 15:37:36.578 Track[2235:707] request:didReceiveResponse: 
2011-10-18 15:37:36.584 Track[2235:707] The operation couldn’t be completed. (facebookErrDomain error 10000.) 
2011-10-18 15:37:36.586 Track[2235:707] Err details: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0x1ce480 {error=<CFBasicHash 0x1c9560 [0x3e368630]>{type = mutable dict, count = 2, 
entries => 
    2 : <CFString 0x19a830 [0x3e368630]>{contents = "type"} = <CFString 0x11d230 [0x3e368630]>{contents = "OAuthException"} 
    3 : <CFString 0x1f5b60 [0x3e368630]>{contents = "message"} = <CFString 0x1f8a70 [0x3e368630]>{contents = "An active access token must be used to query information about the current user."} 

我欣賞的幫助,我真的希望我沒有錯過在這裏解決了這個問題的討論。這裏有幾個(許多的),我已經通過閱讀並找到解決辦法

How to implement Single Sign On (SSO) using facebook ios sdk for iphone Facebook SSO and request iOS SDK Facebook API for iPhone Error: An active access token must be used

回答

0

確保沒有成功的Facebook應用程序(你在Facebook上創建的實際應用)不處於沙盒模式,並且如果確保用戶您的Facebook應用程序(iPhone Facebook客戶端)當前已登錄,則該應用程序是該應用程序的開發人員。

+0

我剛剛檢查,應用程序不在沙盒模式,我絕對是該應用程序的開發人員。這看起來更像是一個問題,我的應用程序沒有閱讀從Facebook應用程序本身傳回的信息 –

+0

我之所以這麼說,是因爲你說Facebook在切換到它之後的幾秒鐘內將您重定向到應用程序。據我所知,正常流量會要求您接受所需的權限,或者如果您之前已經接受了它們,請點擊「確定」。嘗試從用戶個人資料中刪除Facebook應用程序(請參閱隱私設置 - >應用程序和網站),然後再次運行您的應用程序。正如我所說,Facebook不應該切換回你的應用程序,沒有任何來自你的輸入... –