【问题标题】:Querying Facebook user data through new iOS6 social framework通过新的 iOS6 社交框架查询 Facebook 用户数据
【发布时间】:2015-07-11 12:24:58
【问题描述】:

我正在尝试使用 iOS 6 的新 Facebook 集成 API 查询有关用户的信息。这是我正在使用的代码,与他们在 WWDC 上演示的基本相同:

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

    });
}];
}

问题是我从 Facebook 收到错误代码 2500:“必须使用活动访问令牌来查询有关当前用户的信息。”如果我将查询更改为 https://graph.facebook.com/[facebook id] 那么它工作正常。我假设问题是iOS在通过requestForServiceType发送请求时传递了应用程序的访问令牌而不是用户的访问令牌。我只是不知道如何解决它。显然,对我的用户的 Facebook ID 进行预测和硬编码不是一种选择。有什么建议么?

【问题讨论】:

  • 你得到答案了吗??随着 iOS 6 的发布。

标签: facebook ios6


【解决方案1】:

在参数中添加您的活动访问令牌,例如

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

【讨论】:

  • 如何在代码中获取访问密钥?还是您建议在其中放置一个实际的访问代码?似乎访问代码过期了,所以这不是一个好的选择。
【解决方案2】:

我遇到了同样的问题并找到了解决方法:

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

【讨论】:

【解决方案3】:

好的,这就是我如何与 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。

【讨论】:

    【解决方案4】:

    我收到了同样的错误消息,我在更新凭据后通过保存我的帐户来修复它。

    【讨论】:

      【解决方案5】:

      确保您的 (ACAccount *)facebookAccount(或在您的情况下为 self.account) 对象很强大,并且您在获得权限时正确设置它。

      【讨论】:

        【解决方案6】:

        答案很简单

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

        还有函数-(void)facebookProfile

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

        请注意,您需要的参数已添加为字典 请参阅下面的完整列表 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 {
        
            }
        }];
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-30
          • 2013-03-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多