如果你已經知道如何獲得帳戶存儲設置,這裏是它周圍的代碼展示瞭如何獲得訪問令牌:
@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");
}
}];
不幸的是,我們不能使用[self.fbAccount憑證]; - 此方法返回null - 僅查看文檔 - http://developer.apple.com/library/ios/#documentation/Accounts/Reference/ACAccountClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011019 - 出於隱私原因,該帳戶保存後此屬性無法訪問。 –
有趣的是,我可以在模擬器和真實設備上打印出來,但我沒有明確保存它,似乎它可能不會隱式發生。你是否保存它,如果是這樣,爲什麼你需要保存? –
我想在Facebook iOS SDK中使用它。例如,如果我在系統中登錄,我不想使用Facebook iOS SDK再次登錄,我只需要檢索系統訪問令牌並將其保存到Facebook iOS SDK中以供將來的請求使用。 –