How do I create a test user for a native iOS app?
Asked Answered
O

5

9

I'm trying to test my native iOS app and I've been having huge problems with test users. Basically it seems that the normal graph based way of creating test users doesn't work for native apps. When I try I get the following response from the server:

{
    "error": {
        "message": "(#15) This method is not supported for native apps",
        "type": "OAuthException",
        "code": 15
    }
}

This seems to be supported by various posts on SO and a page on the old FB forums:

http://forum.developers.facebook.net/viewtopic.php?id=93086

People say that the only way to create test users for native apps is to create proper fake accounts on FB, which is against FB terms and conditions. Is this really my only option ? I can't believe the FB devs cannot support test accounts for native apps.

Anyone know of any legitimate way to create native app test users ?

Osis answered 15/2, 2012 at 12:46 Comment(2)
Does this answer https://mcmap.net/q/1321211/-how-to-get-a-facebook-test-user-token-programmatically-for-a-native-app from my related question about getting the test users tokens help any?Ornamental
I was able to get this to work, but only if I log in via web, not via the social settings in iOS.Atmospherics
D
1

At the top of the developer documentation for test users, a GUI for maintaining test users is also mentioned.

In addition to the Graph API functionality described below for managing test users programmatically, there is also a simple GUI in the Developer App, available on your app's Roles page as shown in the screenshots below. It exposes all of the API functions described in this document.

For some reason I don't understand, the Graph API calls aren't available if your app has an 'App Type' (under Settings -> Advanced) of Native/Desktop. But you can use the GUI instead.

Daren answered 12/12, 2012 at 15:11 Comment(1)
Realised that the reason you can't use the Graph API if you're a native/desktop app is that your secret is in source code that could be decompiled by others. This would enable them do create test users using the Graph API (probably something that you wouldn't want.Daren
V
0

You can create a new facebook test user for your app by calling the below function.

Please user your APP ID (go to this link to get your App's ID:https://developers.facebook.com/apps)

Please go to your app on facebook and get the App secret ID

-(void)createNewUser{
NSString *appID=@"Past your App ID"
NSString *appSecret=@"Past your App secret"
NSDictionary *params = @{
                             @"true": @"installed",
                             @"access_token": @"appID|appSecret",
                             };



    NSString *path = [NSString stringWithFormat:@"/appID/accounts/test-users"];

    /* make the API call */
    [FBRequestConnection startWithGraphPath:path   
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              )
     {

         if (result  && !error)
         {
             NSLog(@"Test-User created successfully: %@", result);
         }
         else
         {
             NSLog(@"Error creating test-user: %@", error);
             NSLog(@"Result Error: %@", result);
         }

     }];
}

Please make sure you don't reveal your App secret ID to anyone and never include it in your app hardcoded like that.

Hope it helps

Vallombrosa answered 2/5, 2015 at 23:58 Comment(0)
M
0

The following works for me using the Facebook iOS SDK v4.1.0:

// Listed as "App ID": https://developers.facebook.com/apps/
NSString *facebookAppId = @"1234_REPLACE_ME_5678";
// Listed as "App Token": https://developers.facebook.com/tools/accesstoken/
NSString *facebookAppToken = @"ABCD_REPLACE_ME_1234";

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
        initWithGraphPath:[NSString stringWithFormat:@"/%@/accounts/test-users", 
                          facebookAppId]
               parameters:@{@"installed" : @"true"}
              tokenString:facebookAppToken
                  version:@"v2.3"
               HTTPMethod:@"POST"];

[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
        NSDictionary *testUser,
        NSError *facebookError) {
    if (facebookError) {
        NSLog(@"Error creating test user: %@", facebookError);
    } else {
        NSLog(@"access_token=%@", testUser[@"access_token"]);
        NSLog(@"email=%@", testUser[@"email"]);
        NSLog(@"user_id=%@", testUser[@"id"]);
        NSLog(@"login_url=%@", testUser[@"login_url"]);
        NSLog(@"password=%@", testUser[@"password"]);
    }
}];

When successful, the output is then something like this:

access_token=CACQSQXq3KKYeahRightYouDontGetToLookAtMyAccessTokenspBkJJK16FHUPBTCKIauNO8wZDZD
[email protected]
user_id=1384478845215781
login_url=https://developers.facebook.com/checkpoint/test-user-login/1384478845215781/
password=1644747383820
Morbidezza answered 6/5, 2015 at 2:7 Comment(0)
Q
0

Swift4 version.

import FBSDKCoreKit

// App Token can be found here: https://developers.facebook.com/tools/accesstoken/
let appToken = "--EDITED--"
let appID = "--EDITED--"

let parameters = ["access_token": appID + "|" + appToken, "name": "Integration Test"]
let path = "/\(appID)/accounts/test-users"
guard let request = FBSDKGraphRequest(graphPath: path, parameters: parameters, httpMethod: "POST") else {
   fatalError()
}
request.start { _, response, error in
   if let error = error {
      // Handle error.
      return
   }

   if let response = response as? [AnyHashable: Any],
      let userID = response["id"] as? String,
      let token = response["access_token"] as? String,
      let email = response["email"] as? String {
      // Handle success response.
   } else {
      // Handle unexpected response.
   }
}
Quoit answered 8/11, 2017 at 12:51 Comment(0)
G
-1

You can use Facebook's Test User API https://developers.facebook.com/docs/test_users/ to create test users, just be sure to use the same App id.

Given a APP_ID and APP_SECRET, the following gives us an APP_ACCESS_TOKEN

curl -vv 'https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials'

then we can use the token to create test users:

curl 'https://graph.facebook.com/APP_ID/accounts/test-users?installed=true&name=NEW_USER_NAME&locale=en_US&permissions=read_stream&method=post&access_token=APP_ACCESS_TOKEN

in the response body to this command will the new users' email and password.

Gory answered 21/8, 2012 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.