获取Facebook好友返回空

时间:2015-06-29 08:08:01

标签: ios facebook facebook-graph-api

我正在尝试为我的应用获取朋友列表,但这是控制台返回的内容:

    {
    data =     (
    );
    summary =     {
        "total_count" = 1221;
    };

总数是正确的,但它没有告诉我任何朋友?

我在Facebook开发者门户网站上创建了两个测试用户,他们都使用我的应用程序登录,他们是我在Facebook上的朋友,不应该显示他们的数据吗?

以下是我的请求权限的代码,每次用户登录时都会调用此类。

@implementation UploadUser

NSString *userName;
NSString *userId;
NSString *userGender;
//UIImage *userPicture;
NSString *userPicture;

NSMutableURLRequest *urlRequest;

//NSData *imageData;
//NSData *pictureData;
//NSData *tempData;

//Retrieves users name, gender and id
-(void)getUserInfo {

    FBSDKGraphRequest *requestUser = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];

    FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

    [connection addRequest:requestUser completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        userName = result[@"name"];
        userGender = result[@"gender"];
        userId = result[@"id"];

        if (connection) {

            if (!error) {

                [self setUserData];
                [self uploadUser];

            }
        }else if (!connection) {
            NSLog(@"Get User %@",error);
        }
    }];
    [connection start];


    //Get Friends
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                  initWithGraphPath:@"/me/friends"
                                  parameters:nil
                                  HTTPMethod:@"GET"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                          id result,
                                          NSError *error) {
        if (connection) {
            NSLog(@"Friends = %@",result);


        }else if (!connection) {
              NSLog(@"Get Friends Error");
        }
    }];



}


//Saves user data to class
-(void)setUserData {
    DataManager *dm = [DataManager sharedInstance];
    [dm setObject:userName forKey:@"userName"];
    [dm setObject:userGender forKey:@"userGender"];
    [dm setObject:userId forKey:@"userId"];



    NSLog(@"%@",userId);
    NSLog(@"%@",userName);
    NSLog(@"%@",userGender);
}

//Uploads user data online
-(void)uploadUser {
    PFUser *user = [PFUser currentUser];
    user[@"name"] = userName;
    user[@"id"] = userId;
    user[@"gender"] = userGender;
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"User Data Uploaded :)");
        } else {
            //Handle Error
            NSLog(@"User Upload Error... %@",error);
        }
    }];
}

为什么我的查询不回复我的朋友?是因为我的朋友无权使用我的应用程序?我的权限是不是被正确询问了?

3 个答案:

答案 0 :(得分:1)

从图谱API的v2.0开始,您只能获得使用user_friends授权您的应用的朋友。这已成为无数Stackoverflow线程的主题,例如:Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

答案 1 :(得分:1)

您还可以使用Graph API Explorer快速检查您的请求是否有效: https://developers.facebook.com/tools/explorer/

如果你想与朋友联系,你应该问你的朋友也使用这个链接并获得令牌。通过这种方式,facebook系统了解您的朋友在Graph API应用程序中登录,您将获得朋友列表。

答案 2 :(得分:0)