2012-12-18 15 views
22

Devo recuperare il token di accesso di Facebook del mio account di sistema, che ho impostato in Applicazione Impostazioni.Ottieni token di accesso di Facebook da Social.framework (iOS6)

So che Social.framework (iOS6) conosce tutte le informazioni sul mio account FB & posso eseguire chiamate API al Graph API utilizzando la classe SLRequest, come in questo post http://blogs.captechconsulting.com/blog/eric-stroh/ios-6-tutorial-integrating-facebook-your-applications

Ma, la mia domanda è - come faccio recuperare il token di accesso dell'account di sistema di Facebook?

ho trovato la soluzione a Twitter https://dev.twitter.com/docs/ios/using-reverse-auth, ma mi può aiutare con Facebook

risposta

24

Se sai già come ottenere l'archivio account impostato, ecco il codice intorno ad esso mostrando come ottenere il token di accesso:

@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

Purtroppo non è possibile utilizzare [credenziale self.fbAccount]; - questo metodo restituisce null - basta consultare i documenti - http://developer.apple.com/library/ios/#documentation/Accounts/Reference/ACAccountClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011019 - Per motivi di privacy, questa proprietà non è accessibile dopo aver salvato l'account. –

+0

Interessante, sono riuscito a stamparlo sia sul simulatore che sul dispositivo reale ma non lo sto salvando esplicitamente e sembra che non stia accadendo implicitamente. Lo stai salvando e se sì, perché hai bisogno di salvare? –

+0

Mi piacerebbe usarlo nell'SDK di Facebook iOS. Ad esempio, se ho effettuato l'accesso al sistema, non voglio effettuare nuovamente l'accesso utilizzando Facebook iOS SDK, ho solo bisogno di recuperare il token di accesso al sistema e salvarlo su Facebook iOS SDK per richieste future. –

Problemi correlati