2012-06-30 49 views
8

我想查詢有關使用iOS 6的新Facebook集成API的用戶的信息。這是我使用的代碼,這基本上是相同的,他們演示了在WWDC什麼:通過新的iOS6社交框架查詢Facebook用戶數據

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

    }); 
}]; 
} 

問題是我收到的錯誤代碼2500從Facebook:「一個積極的訪問令牌,必須使用查詢有關信息當前用戶「。如果我將查詢更改爲https://graph.facebook.com/ [facebook id],那麼它工作正常。我假設問題是iOS在通過requestForServiceType發送請求時傳遞應用程序的訪問令牌而不是用戶的訪問令牌。我只是不知道如何解決它。很明顯,對我的用戶的Facebook ID進行預測和硬編碼不是一種選擇。有什麼建議麼?

+0

你得到的答案?由於iOS 6的發佈。 –

回答

5

添加您的積極的訪問令牌在paramete像

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

你如何在代碼中訪問鍵?或者你是否建議在那裏設置一個實際的訪問代碼?看起來像訪問代碼到期,所以這不是一個好的選擇。 – zeeple

0

我有同樣的錯誤信息,我通過更新憑證後,救了我的帳戶固定它。

2

我面臨着同樣的問題,找到了解決方法:

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

問題出在FB上的應用程序設置不正確。我根據這個帖子改設置http://stackoverflow.com/questions/12644229/ios-6-facebook-posting-procedure-ends-up-with-remote-app-id-does-not-match-stor/12687305 #12687305它解決了這個問題。 – gareg

0

確保您(ACAccount *)facebookAccount(或你的情況self.account) 對象是強大的,你正確設置同時獲得權限。這裏

1

OK是怎麼做到這一點的整合與iOS 6,並得到我想要的東西從Facebook:

在AppDelegate中我這樣做:

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

,並在我的ViewController,我想檢索信息我自己或我的朋友,我這樣做(注:這是一個測試,所以這是一個很大的權限!):

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


             } 
            }]; 

然後在「presentPostOptions」我這樣做(在這個例子中,我嘗試檢索從我的朋友的東西):

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

我不知道你想做些什麼,因爲你的問題,你只是試圖做一個連接,但這樣一來,你可以做你想做什麼都當你是集成在iOS 6中。

還有一件事,確保你的應用程序設置通過Facebook和那裏的配置,如啓用你的應用程序的iOS和iPhone/iPad的ID。還有你的plist中的FacebookAppID。

讓我知道,如果它爲你,

編輯:順便說一句我的Facebook SDK 3.1.1是。

0

答案很簡單

在viewDidLoad中

()使用方法:

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

而且功能 - (無效)facebookProfile

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

通知您需要的PARAMS被添加作爲字典 參閱以下完整清單 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 { 

    } 
}]; 


} 
相關問題