2012-06-30 5 views
8

Ich versuche, Informationen über einen Benutzer mithilfe der neuen Facebook-Integrations-API von iOS 6 abzufragen. Dies ist der Code ich verwende, die im Wesentlichen identisch ist, was sie auf der WWDC demoed:Abfrage von Facebook-Benutzerdaten über das neue soziale Framework iOS6

{ 
NSDictionary *parameters = @{}; 
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
             requestMethod:SLRequestMethodGET 
                URL:url 
              parameters:parameters]; 
request.account = self.account; 
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
    NSLog(@"Facebook request received, status code %d", urlResponse.statusCode); 
    NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response data: %@", response); 
    dispatch_async(dispatch_get_main_queue(), ^{ 

    }); 
}]; 
} 

Problem ist Ich erhalte Fehlercode 2500 von Facebook: „Ein aktiver Zugriffstoken muss verwendet werden, um Informationen abzufragen, zu der aktuelle Benutzer. " Wenn ich die Abfrage in https://graph.facebook.com/[facebook id] ändere, funktioniert es gut. Ich nehme an, dass das Problem ist, dass iOS das Zugriffstoken der App anstelle des Zugriffstokens des Benutzers übergibt, wenn die Anfrage über requestForServiceType gesendet wird. Ich weiß einfach nicht, wie ich das beheben soll. Offensichtlich antizipieren und hartcodieren die Facebook IDs meiner Benutzer ist keine Option. Irgendwelche Vorschläge?

+0

Haben Sie die Antwort ?? Als iOS 6 ist freigegeben. –

Antwort

5

aktiven Zugriffstoken in paramete wie

NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"PUT_ACCESS_TOKEN_HERE" forKey:@"access_token"]; 
+0

Wie erhalten Sie diesen Zugriffsschlüssel im Code? Oder schlagen Sie vor, einen tatsächlichen Zugangscode dort zu setzen? Scheint wie Zugriffscodes ablaufen, so dass dies keine gute Option wäre. – zeeple

0

füge ich die gleiche Fehlermeldung hatte, habe ich es repariert mein Konto durch Speichern nachdem die Anmeldeinformationen zu erneuern.

2

stand ich mit dem gleichen Problem und fand die Lösung:

NSString *uid = [NSString stringWithFormat:@"%@", [[self.account valueForKey:@"properties"] valueForKey:@"uid"]] ;     
NSURL *url = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:uid]]; 
+0

Das Problem war in falschen App-Einstellungen auf der FB. Ich habe die Einstellungen nach diesem Beitrag http://stackoverflow.com/questions/12644229/ios-facebook-posting-procedure-ends-up-with-remote-app-id-does-not-match-stor/12687305 geändert # 12687305 und es wurde das Problem behoben. – gareg

0

Stellen Sie sicher, Ihre (ACAccount *) facebookAccount (oder in Ihrem Fall self.account) Objekt ist stark und Sie setzen es richtig während Berechtigungen erhalten.

1

OK hier ist, wie ich tun, um diese Integration mit iOS 6 und bekommen, was ich von Facebook wollen:

In AppDelegate ich dies tun:

- (BOOL)application:(UIApplication *)application 
      openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation { 
    return [FBSession.activeSession handleOpenURL:url]; 
} 
- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    [FBSession.activeSession handleDidBecomeActive]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    [FBSession.activeSession close]; 
} 

und in meinem Viewcontroller, wo ich Informationen abrufen möchten über mich oder meine Freunde, die ich tun, um diese (Hinweis: dies ist ein Test, so ist es viel Berechtigungen!):

NSArray *permissions = 
    [NSArray arrayWithObjects:@"email", @"user_location", 
    @"user_birthday", 
    @"user_likes", @"user_interests",@"friends_interests",@"friends_birthday",@"friends_location",@"friends_hometown",@"friends_photos",@"friends_status", 
    @"friends_about_me", @"friends_birthday", @"friends_hometown", @"friends_interests", @"friends_likes", @"friends_location", nil]; 

    [FBSession openActiveSessionWithReadPermissions:permissions 
             allowLoginUI:YES 
            completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { 
             /* handle success + failure in block */ 
             if (status) { 
              NSLog(@"Facebook Read Permission is successful!"); 
              [self presentPostOptions]; 

              // [self presentPostOptions]; 


             } 
            }]; 

Dann in „presentPostOptions“ ich tue dies (in diesem Beispiel versuche ich abrufen etwas von meinem Freund):

- (void)presentPostOptions 
{ 

    [[FBRequest requestForMyFriends] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) 

    { 

     if (!error) { 

NSArray *data = [user objectForKey:@"data"]; 
      NSLog(@"%d", [data count]); 
      for (FBGraphObject<FBGraphUser> *friend in data) { 
       NSLog(@"%@", [friend first_name]); 
       NSLog(@"%@", [friend last_name]); 
       NSLog(@"%@", [friend id]); 
       //make sure you have FBProfilePictureView outlet in your view 
       //otherwise skip the profile picture! 
       self.fbProfilePic.profileID = @"you'r friend's profile.id"; 

      } 
     } 
     else 
     { 
      NSLog(@"error"); 
      // [self didFailWithError:error]; 
     } 
    }]; 

Ich weiß nicht, was sonst Sie denn in Ihrer Frage tun möchten, um eine Verbindung mit Ihnen gerade versucht, machen aber auf diese Weise die Sie tun können, was immer Sie wollen, während Sie in iOS 6 integriert.

Noch eine Sache, stellen Sie sicher, über Ihre App-Einstellungen über Facebook und die Konfigs drüben wie aktivieren Sie Ihre App für iOS und die ID für iPhone/iPad. Auch die FacebookAppID in deinem Plist.

Lassen Sie mich wissen, ob es für Sie arbeitet,

EDIT: btw ist mein Facebook-SDK 3.1.1.

0

Die Antwort ist einfach

in viewDidLoad() verwenden:

accountStore= [[ACAccountStore alloc]init]; 
facebookAccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

NSDictionary *options= @{ 
ACFacebookAudienceKey: ACFacebookAudienceEveryone, 
ACFacebookAppIdKey: @"<YOUR FACEBOOK APP ID>", 
ACFacebookPermissionsKey: @[@"public_profile"] 

          };  

[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) { 

    if (granted) { 
     NSLog(@"Permission has been granted to the app"); 
     NSArray *accounts= [accountStore accountsWithAccountType:facebookAccountType]; 
     facebookAccount= [accounts firstObject]; 
     [self performSelectorOnMainThread:@selector(facebookProfile) withObject:nil waitUntilDone:NO]; 

    } else { 
     NSLog(@"Permission denied to the app"); 
    } 
}]; 

Und die funktions- (void) facebookProfile

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

Beachten Sie die params Sie müssen als Wörterbuch hinzugefügt Siehe unten für vollständige Liste https://developers.facebook.com/docs/graph-api/reference/user

NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name",@"fields", nil]; 

SLRequest *profileInfoRequest= [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:param]; 
profileInfoRequest.account= facebookAccount; 


[profileInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
    NSLog(@"Facebook status code is : %ld", (long)[urlResponse statusCode]); 

    if ([urlResponse statusCode]==200) { 

     NSDictionary *dictionaryData= [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; 



    } else { 

    } 
}]; 


}