0

我们需要我们所有的测试用户成为彼此的朋友。根据您需要的测试用户数量(在我们的例子中超过 50 个测试用户),通过 App Dashboard 手动执行此操作是一项巨大的工作。

因此,我们正在寻找一种方法让我们的 Facebook 以编程方式测试用户彼此的朋友。我们在这里按照他们的网站尝试了这种方法:https ://developers.facebook.com/docs/graph-api/reference/v2.0/test-user/friends

问题是,为了从测试用户 1 向测试用户 2 发送好友请求,您必须使用测试用户 1 登录,并且为了接受好友请求,您需要使用测试用户 2 登录,这使得过程甚至比使用 App Dashboard -> Roles 手动添加更糟糕

我们如何使用 iOS SDK 3.14.1 以编程方式让所有测试用户成为朋友?

4

2 回答 2

0

如果您以编程方式创建用户,您可以轻松地让他们彼此成为朋友。

https://developers.facebook.com/docs/graph-api/reference/v2.1/test-user/friends

#import "FBGraphObject.h"

@protocol FBTestGraphUser <FBGraphObject>

@property (nonatomic, readonly) NSString *id;
@property (nonatomic, readonly) NSString *access_token;
@property (nonatomic, readonly) NSString *login_url;
@property (nonatomic, readonly) NSString *email;
@property (nonatomic, readonly) NSString *password;
@property (nonatomic, retain) NSArray *friends;

@end


-(id<FBTestGraphUser>)createTestFacebook
{
    NSString *appName = "";
    NSString *userPrefix = [NSString stringWithFormat:@"%@User", appName];
    NSString *facebookApplicationId = "";
    NSString *facebookApplicationAccessToken = "";
    NSString *url = [NSString stringWithFormat:@"https://graph.facebook.com/%@/accounts/test-users?installed=true&name=%@&locale=en_US&permissions=email,user_birthday,publish_actions&access_token=%@", facebookApplicationId, userPrefix, facebookApplicationAccessToken];    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    return (id<FBTestGraphUser>)[FBGraphObject graphObjectWrappingDictionary:[data objectFromJSONData]];
}

-(void)deleteTestFacebookUser:(id<FBTestGraphUser>)testFacebookUser
{
    NSLog(@"Deleting Facebook users...");

    NSMutableArray* existingUsers = [NSMutableArray arrayWithArray:testFacebookUser.friends];
    [existingUsers addObject:testFacebookUser];

    NSOperationQueue* wipeUsers = [NSOperationQueue new];

    [existingUsers enumerateObjectsUsingBlock:^(id<FBTestGraphUser> user, NSUInteger idx, BOOL *stop) {
        [wipeUsers addOperationWithBlock:^{
            [self deleteTestFacebookUser:user];
        }];
    }];

    [wipeUsers waitUntilAllOperationsAreFinished];

    NSLog(@"Done deleting Facebook users");
}

-(void)makeUser:(id<FBTestGraphUser>)userA friendsWithUser:(id<FBTestGraphUser>)userB {
    // Don't try to parallelize this; you'll get unknown OAuth exceptions.  -CS 2013-11-18
    [self sendFriendRequestFrom:userA toUser:userB];
    [self sendFriendRequestFrom:userB toUser:userA];
}

-(void)sendFriendRequestFrom:(id<FBTestGraphUser>)sender toUser:(id<FBTestGraphUser>)receiver {
    NSString *url = [NSString stringWithFormat:@"https://graph.facebook.com/%@/friends/%@?access_token=%@", sender.id, receiver.id, sender.access_token];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    NSURLResponse *response = nil;
    NSError *error = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}

-(void)deleteTestFacebookUser:(id<FBTestGraphUser>)testFacebookUser
{
    NSString *url = [NSString stringWithFormat:@"https://graph.facebook.com/%@?access_token=%@", testFacebookUser.id, WBTestCaseFacebookAppID];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"DELETE"];
    NSError *error = nil;
    NSURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}
于 2014-09-10T18:33:27.373 回答
0

使用 Web 服务器最容易做到这一点。

例如使用facebook-node-sdk

  1. 创建用户 FB.api('/v2.6/{app-id}/accounts/test-users', 'post', { fields: [{ installed: "true", permissions: "user_birthday user_friends email"}] }, function (res) { ... });

  2. 保存新创建的用户 id 和 access_token

  3. 根据需要重复步骤 1-2

  4. 用户A向用户B发送好友请求 FB.api('/v2.6/' + userA.fb_id + '/friends/' + userB.fb_id, { access_token: userA.access_token }, 'post', function (res) { ... });

  5. 将用户B的好友请求发送给用户A以接受 FB.api('/v2.6/' + userB.fb_id + '/friends/' + userA.fb_id, { access_token: userB.access_token }, 'post', function (res) { ... });

cf连接朋友边

于 2016-07-08T00:54:16.850 回答