【问题标题】:How to add test friends to Facebook test users programmatically using Facebook iOS SDK 3.14.1如何使用 Facebook iOS SDK 3.14.1 以编程方式将测试好友添加到 Facebook 测试用户
【发布时间】:2016-07-08 08:54:16
【问题描述】:

我们需要我们所有的测试用户成为彼此的朋友。根据您需要的测试用户数量(在我们的例子中超过 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 以编程方式让所有测试用户成为朋友?

【问题讨论】:

    标签: ios facebook facebook-ios-sdk facebook-test-users


    【解决方案1】:

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

    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】:

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

      例如使用 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. 从 userA 向 userB 发送好友请求 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) { ... });

      参考Connecting friend edges

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多