2012-12-18 126 views
22

我需要檢索我在系統設置應用程序中設置的系統帳戶的Facebook訪問令牌。從Social.framework(iOS6)獲取Facebook訪問令牌

我知道Social.framework(iOS6的)知道我可以執行API的所有我的FB帳戶信息&調用這個帖子http://blogs.captechconsulting.com/blog/eric-stroh/ios-6-tutorial-integrating-facebook-your-applications

但使用SLRequest類,喜歡圖形API,我的問題是 - 我怎麼能檢索我的Facebook系統帳戶的訪問令牌?

我找到了解決方案,Twitter的https://dev.twitter.com/docs/ios/using-reverse-auth,但你能不能幫我與Facebook

回答

24

如果你已經知道如何獲得帳戶存儲設置,這裏是它周圍的代碼展示瞭如何獲得訪問令牌:

@property (strong, nonatomic) ACAccountStore *accountStore; 
@property (strong, nonatomic) ACAccount *fbAccount; 

... 

@synthesize accountStore = _accountStore; 
@synthesize fbAccount = _fbAccount; 

... 

// Initialize the account store 
self.accountStore = [[ACAccountStore alloc] init]; 

// Get the Facebook account type for the access request 
ACAccountType *fbAccountType = [self.accountStore 
    accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

// Request access to the Facebook account with the access info 
[self.accountStore requestAccessToAccountsWithType:fbAccountType 
              options:fbInfo 
             completion:^(BOOL granted, NSError *error) { 
    if (granted) { 
     // If access granted, then get the Facebook account info 
     NSArray *accounts = [self.accountStore 
          accountsWithAccountType:fbAccountType]; 
     self.fbAccount = [accounts lastObject]; 

     // Get the access token, could be used in other scenarios 
     ACAccountCredential *fbCredential = [self.fbAccount credential];    
     NSString *accessToken = [fbCredential oauthToken]; 
     NSLog(@"Facebook Access Token: %@", accessToken); 


     // Add code here to make an API request using the SLRequest class 

    } else { 
     NSLog(@"Access not granted"); 
    } 
}]; 
+3

不幸的是,我們不能使用[self.fbAccount憑證]; - 此方法返回null - 僅查看文檔 - http://developer.apple.com/library/ios/#documentation/Accounts/Reference/ACAccountClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011019 - 出於隱私原因,該帳戶保存後此屬性無法訪問。 –

+0

有趣的是,我可以在模擬器和真實設備上打印出來,但我沒有明確保存它,似乎它可能不會隱式發生。你是否保存它,如果是這樣,爲什麼你需要保存? –

+0

我想在Facebook iOS SDK中使用它。例如,如果我在系統中登錄,我不想使用Facebook iOS SDK再次登錄,我只需要檢索系統訪問令牌並將其保存到Facebook iOS SDK中以供將來的請求使用。 –