How to add test friends to Facebook test users programmatically using Facebook iOS SDK 3.14.1

谁说胖子不能爱 提交于 2021-01-29 19:40:33

问题


We need that all our test users to be friends of each other. Doing that through the App Dashboard manually is a tremendous amount of work depending on the number of test users you need (in our case more than 50 test users).

Therefore we are looking for a way to make our Facebook test users friends of each other programmatically. We tried this approach following their website here: https://developers.facebook.com/docs/graph-api/reference/v2.0/test-user/friends

The problem is that in order to send a friend request from test user one to test user two you have to be logged in with test user one, and in order to accept the friend request you need to login with test user two, which makes the process even worse than adding manually using the App Dashboard -> Roles

How can we make all our test users friend of each other programmatically using iOS SDK 3.14.1?


回答1:


If you create your users programmatically you can easily make them friends with one another.

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



回答2:


It would be easiest to do this with a web server.

E.g. Using the facebook-node-sdk:

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

  2. Save newly created user id and access_token

  3. Repeat steps 1-2 as desired

  4. Send friend request from userA to userB FB.api('/v2.6/' + userA.fb_id + '/friends/' + userB.fb_id, { access_token: userA.access_token }, 'post', function (res) { ... });

  5. Send friend request from userB to userA to accept FB.api('/v2.6/' + userB.fb_id + '/friends/' + userA.fb_id, { access_token: userB.access_token }, 'post', function (res) { ... });

C.f. Connecting friend edges



来源:https://stackoverflow.com/questions/24046772/how-to-add-test-friends-to-facebook-test-users-programmatically-using-facebook-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!