如何获取Facebook提供数据

时间:2016-02-09 12:12:57

标签: ios objective-c facebook-graph-api fbsdk

我正在尝试登录用户Feed,用户自己在墙上显示的Feed或显示给朋友的最新Feed。

我使用旧程序检查了所有文章和其他帖子,以获取Facebook所描述的Feed。 现在我正在使用这种方法但是没有得到任何东西

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"access_token"] = [FBSDKAccessToken currentAccessToken].tokenString;



FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:@"/me/feed"
                              parameters:parameters
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
     if (error) {
         NSLog(@"%@",error);
     }else{
        // NSArray* posts = [result objectForKey:@"data"];
     }
}];

1 个答案:

答案 0 :(得分:3)

请使用以下代码在Facebook上分享文字。 在按钮单击事件上调用以下方法。

- (void)shareOnFacebook{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if ([FBSDKAccessToken currentAccessToken] != nil)
    {
        NSDictionary *dict =  @{@"message":sharingMsg};
        FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/me/feed" parameters:dict HTTPMethod:@"POST"];
        [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
         {
             [MBProgressHUD hideHUDForView:self.view animated:YES];
             if (error != nil)
                 NSLog(@"%@",error.localizedDescription);
             else
                 NSLog(@"Success");
         }];
    }
    else{
        FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
        [loginManager setLoginBehavior:FBSDKLoginBehaviorSystemAccount];
        [loginManager logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
         {
             [MBProgressHUD hideHUDForView:self.view animated:YES];
             if (error)
                 [loginManager logOut];
             else if (result.isCancelled)
                 [loginManager logOut];
             else
             {
                 if ([result.grantedPermissions containsObject:@"publish_actions"])
                 {
                     [self grantPermissionFromFB];
                 }
                 else
                 {
                     [loginManager logInWithPublishPermissions:@[@"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
                      {
                          [MBProgressHUD hideHUDForView:self.view animated:YES];
                          if (error)
                              [loginManager logOut];
                          else if (result.isCancelled)
                              [loginManager logOut];
                          else
                          {
                              [self grantPermissionFromFB];
                          }
                      }];
                 }
             }
         }];
    }
}

- (void)grantPermissionFromFB{
    NSTimeInterval addTimeInterval = 60*60*24*365*50;
    NSDate *expireDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];
    NSDate *refreshDate = [[NSDate date] dateByAddingTimeInterval:addTimeInterval];

    FBSDKAccessToken *newAccessToken = [[FBSDKAccessToken alloc] initWithTokenString:[[FBSDKAccessToken currentAccessToken] tokenString] permissions:nil declinedPermissions:nil appID:facebookAppID userID:[[FBSDKAccessToken currentAccessToken] userID] expirationDate:expireDate refreshDate:refreshDate];
    [FBSDKAccessToken setCurrentAccessToken:newAccessToken];

    NSDictionary *dict = @{@"message":sharingMsg};
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/me/feed" parameters:dict HTTPMethod:@"POST"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
     {
         [MBProgressHUD hideHUDForView:self.view animated:YES];
         if (error != nil)
             NSLog(@"%@",error.localizedDescription);
         else
             NSLog(@"Success");
     }];
}