Skip to main content
Box Employee
May 22, 2025 9:04 AM UTC
Question

Questions for BOX App User coding

A.  This is the method used to authenticate the old app which was using standard users and synchronizing to the individual logged in users Box account:
  [BOXContentClient setClientID:@xyz" clientSecret:@abc"];
  [[BOXContentClient defaultClient] authenticateInAppWithCompletionBlock:^(BOXUser *user, NSError *error) {
    if (error == nil) {
      NSLog(@"Logged in user: %@", user.login);
    } else {
      NSLog(@"Error occurred authenticating with Box: %@", error.description);
    }
  }];
 
If I merely replace the first line with the line below, which contains the clientID and clientSecret for the new app, it will not authenticate.
  [BOXContentClient setClientID:@elm" clientSecret:@"jul"];
 
Since we are wanting to use AppUsers instead of standard users, the documentation leads me to believe that we cannot use this method of authenticating with the new app.  Is that the case?
 
B.  The documentation leads you through the various things to do for oAuth2 but there is no clear, concise code sample showing how to create an oAuth2 session and authenticate.  Can you provide?
 
C.  If not, here is the code from some of the various attempts I have tried.  I have imported the JWT pod into the application and tried to use it to create a JWT but when the encodePayload is called, it tries to connect and the Box error screen appears.  It isnt clear from the documentation what I would even do with the JWT if it were created successfully.  Do I need to create a BoxParallelOAuth2Session or can I call the [BoxContentClient defaultClient] authenticateInAppWithCompletionBlock method?  Again, a simple set of example code would be niceusing our clientID, clientSecret, AppID, etc would be even better.
 
Code that doesnt work:
  @try{
    NSString *jti = [Guid newGuidString];
    NSDate *exp = [NSDate date];
    NSDictionary *payload = @{@"iss": @abcdef", @"sub": @***@example.com", @"box_sub_type": @"user", @"aud": @"https://api.box.com/oauth2/token", @"jti": jti, @"exp": exp};
    NSString *secret = @opqrst";

    19 replies

    Box Employee
    May 22, 2025 9:04 AM UTC

    Per Murtzas statement earlier in the thread:

    A. The App User authentication process is slightly different than the standard user authentication process on mobile. For App User authentication, you would have to implement the App User OAuth flow on the server, and then pass the app user access token back to the device.

     

    You can use one of our SDKs like our Java, .NET, or Ruby ones to implement the App User OAuth flow on the server. 

     

    Once you get the app user token back to the device, you would pass the app user access token to the fetchAccessTokenWithCompletion method. 
     
    This seems to conflict with the Box documentation which makes it appear that the first trip should be made to:  https://account.box.com/api/oauth2/authorize.
    So the question is:  to obtain the initial, 30-second-in-duration app token, should the app make the call to the Box URL above or to our server that handles the tokens?
    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

     You would be implementing authentication using JWT (not App User OAuth as I mistakenly wrote in my first response). The JWT auth process is designed to be used with App Users.

     

    I'll first describe how to generate the App User access token using our .NET SDK, and then I'll describe how to use this App User access token in an iOS app to upload a file. 

     

    Before we walk through those steps, I wanted to answer your question about the AppDelegate.m file. If you are using the App User access token, you do not need to import the SDK in the AppDelegate.m file or set the API credentials there.

     

    Here are the steps to generate an App User access token with our .NET SDK.

     

    1. Install the core SDK and JWT support packages using Nuget

     

    PM> Install-Package Box.V2
    PM> Install-Package Box.V2.JWTAuth

     

    2. The SDK expects your Box API credentials to be defined in the boxConfig variable. This page describes where to get these credentials.

     

    var boxConfig = new BoxConfig(, , , ,
    , );
    var boxJWT = new BoxJWTAuth(boxConfig);

     

    3. Query the Box API for an admin token. This admin token will let you create an App User in the next step. 

     

    var adminToken = boxJWT.AdminToken(); 
    

     

    4.  Create a new instance of the admin client using the adminToken from the previous step. Then make an API call to Box to generate a new App User. 

     

    var adminClient = boxJWT.AdminClient(adminToken);
    var userRequest = new BoxUserRequest() { Name = "NAME_FOR_APP_USER", IsPlatformAccessOnly = true };
    var appUser = await adminClient.UsersManager.CreateEnterpriseUserAsync(userRequest);

     

    5. Generate an access token for the App User created in the previous step. This access token will be valid for 60 minutes.

     

    var userToken = boxJWT.UserToken(appUser.Id);

     

    Here are the steps to use the App User access token we generated in the previous section in an iOS app where we want to upload a file to Box.

     

    1. Add the Box iOS SDK to your Podfile. Then run the pod install command to install the SDK.

     

    pod 'box-ios-sdk'
    pod install

     

    2. Import Box iOS SDK in the header file for the class where you will be calling the Box API. 

     

    #import 

     

    3. This class will be implementing a protocol defined by the iOS SDK, so add this protocol to your class interface. 

     

    @interface ViewController : UIViewController 

     

    4. Create a method to query your server for an App User access token. Store this access token in a property that you can access from that class.

     

    @property (strong,nonatomic) NSString *accessToken;
    self.accessToken = [yourCustomMethodToRequestAppUserAccessToken];

     

    5. When you are using the App User access token with the Box iOS SDK, you will be overriding the default authentication defined by the Box iOS SDK. You will do this by initializing the client created by the Box iOS SDK with your own access token. The Box Client will look for the value of the App User access token you get from your server in the fetchAccessTokenWithCompletion method.

     

    - (void)fetchAccessTokenWithCompletion:(void (^)(NSString *, NSDate *, NSError *))completion
    {
    completion(self.accessToken, [NSDate dateWithTimeIntervalSinceNow:100], nil);
    }

     

    6. Now we will create a new Box Client that authenticates with the App User access token from your server. The example method below will take this access token and upload a file to Box and store it in the App User's root folder. Lastly this method will log the file id of the uploaded file.

      

    -(void)uploadFileToBox {
        BOXContentClient *client = [BOXContentClient clientForNewSession];
        [client setAccessTokenDelegate:self];
        
        NSString *localFilePath = [[NSBundle mainBundle] pathForResource:@"file-nae" ofType:@"png"];
        BOXFileUploadRequest *uploadRequest = [client fileUploadRequestToFolderWithID:0 fromLocalFilePath:localFilePath];
        uploadRequest.fileName = @"file-name.png";
        
        [uploadRequest performRequestWithProgress:^(long long totalBytesTransferred, long long totalBytesExpectedToTransfer) {
            // Update a progress bar, etc.
        } completion:^(BOXFile *file, NSError *error) {
            // Upload has completed.
            NSLog(@"%@", file.modelID);
        }];
    }

     

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    Murtza,

    Good stuff and thanks

    One answered question:

     

    The example has the server piece creating an app user and getting the session.  i think we want that left in our app unless theyre treating these app users as a throwaway object that doesnt persist.  if so, then we may have an issue with mapping that app user to appropriate collaboration folders

     

    Please let us know your thoughts on this as soon as you can

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    Just wanted to reiterate that we do not recommend generating the App User access token on a mobile app. For security reasons, the App User access token should be generated on the server and then passed to the mobile app to make requests to the Box API. 

     

    App Users and Managed Users can collaborate on Box files and folders. For example, let's say you want to upload a file to an App User's Box account and then add a Managed User as a collaborator. To do this via the Box API, you would first generate an App User access token on the server. Next, using that access token you would upload the file to the App User's Box account. Finally, you would add the Managed Users as a collaborator to that file using the access token.

     

     

     

     

     

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    MurtzaThanks for this answerWe solved the authentication issue this weekend
    The example you present is exactly the reverse of what we will do with app users and managed users
    The managed users will upload files to the company file structure, and the app users will view those files and then send back reports to a universal folder on the company BOX file structure.
    Any process for this type of interaction that we should know about when coding the app?
    Appreciate it
    Tom

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    Here are the steps to achieve the use case you described:

     

    1. Generate an Enterprise access token on the server.

     

    2. Create an App User with the Enterprise access token on the server.

     

    3. For this App User to view the Managed User's content, you will need to add the App User as a collaborator with the Viewer permission level to the content needed. To do this, make an API call from the server to the Create Collaboration endpoint using the Enterprise access token. In this API call, you will need to set the As-User header to the user id of the Managed User who owns the content.

     

    4. For this App User to add content to Managed User's folder, you will need to add the App User as a collaborator with the Uploader permission level to the folder where you want to upload content. Similar to step 3, make an API call from the server to the Create Collaboration endpoint using the Enterprise access token. In this API call, you will need to set the As-User header to the user id of the Managed User who owns the content.

     

    5. Generate an App User access token on the server for this App User.

     

    6. Pass this App User access token to your mobile app.

     

    7. With this App User access token, you can now view content on the mobile app from Step 3, and you can also upload content to the folder from step 4.

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    How can managed users invite app users to collaborate on specific folders?

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    You can invite an App User to collaborate on a folder owned by a Managed User using the Create Collaboration API. 

     

    To authenticate this API call, you will need to use an Enterprise access token. You will also need to set the As-User header to the user id of the Managed User who owns the content.

     

    Below is an example API call showing how to do this. 

     

     

    curl https://api.box.com/2.0/collaborations \
    -H "Authorization: Bearer ENTERPRISE_ACCESS_TOKEN" \
    -H "As-User: MANAGED_USER_ID" \ -d '{"item": { "id": "FOLDER_ID", "type": "folder"}, "accessible_by": { "id": "APP_USER_ID", "type": "user" }, "role": "viewer"}' \ -X POST

     

    Translate
    Box Employee
    May 22, 2025 9:04 AM UTC

    Murtza,

    We were finally able to get the authentication piece going but now we're getting weird errors as we try to compile the application.  With the application working, I added a new pod into the podfile manifest and ran the pod install command.  Everything seemed fine, but when I came back into the application, I now get compiler errors saying that there is a redefinition of all of the variables in the BoxContentSDK pods.

     

    Can you point me towards what might be the issue?  I'm only referencing the BoxContentSDK through the pod and do not have any of the linked libraries selected .  And I've checked the header search paths and library search paths and don't see any references to a location where the Box SDK would be located on my hard drive.

     

    We are running on a very tight schedule now, due to the time it took us to get authentication working and now we're hitting this issue with Box when we didn't make any changes to anything that should have affected Box.  

     

    Ron Icard

     

    Translate