2012-06-30 35 views
8

Tôi đang cố gắng truy vấn thông tin về người dùng sử dụng API tích hợp Facebook mới của iOS 6. Đây là mã tôi đang sử dụng, về cơ bản giống với những gì họ đã giới thiệu tại WWDC:Truy vấn dữ liệu người dùng Facebook thông qua khung công tác xã hội iOS6 mới

{ 
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(), ^{ 

    }); 
}]; 
} 

Vấn đề là tôi nhận được mã lỗi 2500 từ Facebook: "Mã thông báo truy cập hoạt động phải được sử dụng để truy vấn thông tin về người dùng hiện tại. " Nếu tôi thay đổi truy vấn thành https://graph.facebook.com/[facebook id] thì nó hoạt động tốt. Tôi giả định rằng vấn đề là iOS đang chuyển mã thông báo truy cập của ứng dụng thay vì mã thông báo truy cập của người dùng khi gửi yêu cầu qua requestForServiceType. Tôi chỉ không biết cách sửa nó. Rõ ràng dự đoán và mã hóa cứng ID Facebook của người dùng của tôi không phải là một lựa chọn. Bất kỳ đề xuất?

+0

Bạn có nhận được câu trả lời không? Khi iOS 6 được phát hành. –

Trả lời

5

thêm thẻ truy cập hoạt động của bạn trong paramete như

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

Làm cách nào để bạn nhận được khóa truy cập đó trong mã? Hay bạn đang đề xuất đặt một mã truy cập thực sự trong đó? Có vẻ như mã truy cập hết hạn để không phải là một lựa chọn tốt. – zeeple

0

tôi đã được thông báo lỗi tương tự, tôi cố định nó bằng cách tiết kiệm tài khoản của tôi sau khi đổi mới có khả năng.

2

tôi phải đối mặt với cùng một vấn đề và tìm ra cách giải quyết:

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

Sự cố xảy ra trong cài đặt ứng dụng không chính xác trên FB. Tôi đã thay đổi cài đặt theo bài đăng này http://stackoverflow.com/questions/12644229/ios-6-facebook-posting-procedure-ends-up-with-remote-app-id-does-not-match-stor/12687305 # 12687305 và nó giải quyết được vấn đề. – gareg

0

Hãy chắc chắn rằng bạn (ACAccount *) facebookAccount (hoặc trong trường hợp self.account của bạn) đối tượng là mạnh mẽ và bạn cài đặt nó một cách chính xác trong khi nhận quyền.

1

OK ở đây là làm thế nào tôi làm hội nhập này với iOS 6 và nhận được những gì tôi muốn từ Facebook:

Trong appdelegate Tôi làm điều này:

- (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]; 
} 

và trong ViewController tôi, nơi tôi muốn lấy thông tin về bản thân mình hoặc bạn bè của tôi, tôi làm điều này (Chú ý: đây là một thử nghiệm, vì vậy nó là rất nhiều các điều khoản!):

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]; 


             } 
            }]; 

Sau đó, trong "presentPostOptions" tôi làm điều này (trong ví dụ này tôi cố gắng để lấy một cái gì đó từ bạn tôi):

- (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]; 
     } 
    }]; 

Tôi không biết bạn muốn làm gì khác vì câu hỏi của bạn chỉ là cố gắng tạo kết nối nhưng theo cách này bạn có thể làm những gì bạn muốn được tích hợp trong iOS 6.

Một điều nữa, đảm bảo về Cài đặt ứng dụng của bạn trên Facebook và các cấu hình trên đó như bật ứng dụng cho iOS và ID cho iPhone/iPad. Ngoài ra FacebookAppID trong plist của bạn.

Hãy cho tôi biết nếu nó hoạt động cho bạn,

EDIT: btw SDK Facebook của tôi là 3.1.1.

0

Câu trả lời rất đơn giản

trong viewDidLoad() sử dụng:

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"); 
    } 
}]; 

Và function- (void) facebookProfile

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

Thông báo các params bạn cần được bổ sung như từ điển Tham khảo bên dưới để biết danh sách đầy đủ 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 { 

    } 
}]; 


} 
Các vấn đề liên quan