In my JWT authenticated app, I need the capability to create a user, and immediately upload content to this user's account. While attempting to get a userToken for the newly created user, I get the error:
System.NullReferenceException: Object reference not set to an instance of an object.
Here is a code sample:
//authorize jwt and get admin token
StreamReader reader = new StreamReader(Config.BoxCredentialsFile);
string json = reader.ReadToEnd();
IBoxConfig config = BoxConfig.CreateFromJsonString(json);
SDK = new BoxJWTAuth(config);
string token = SDK.AdminToken();
BoxClient adminClient = SDK.AdminClient(token);
//create new user
var userRequest = new BoxUserRequest()
Login = "***email address removed for privacy***",
Name = Person,
BoxUser user = adminClient.UsersManager.CreateEnterpriseUserAsync(userRequest).Result;
//get user token
string userToken = SDK.UserToken(user.Id); //this is the line that causes the error
userClient = SDK.UserClient(userToken, user.Id);
I don't understand why I'm getting a NullReferenceException. The user is created successfully (confirmed through the admin console), and I can access the attributes of user without issue (including user.Id). Additionally, I have no problem getting a userToken for users who are currently active or are created through the admin console, the issue only occurs for users created through the API.
Has anyone else encountered this issue before?
Please sign in to leave a comment.