Welcome to the new Box Support website. Check out all the details here on what’s changed.

.Net SDK, BoxJWTAuth getting Object reference not set to an instance of an object

Answered
New post

Comments

5 comments

  • dhabed

    Solved the object reference error adding following code:

    var privateKey = File.ReadAllText(privateKeyPath);
    
                    var boxConfig = new BoxConfig(clientId, clientSecret, enterpriseId, privateKey, privateKeyPassword, publicKeyId);
                    var boxJWT = new BoxJWTAuth(boxConfig);

    Now, getting another error on var adminToken = boxJWT.AdminToken(); instruction:

    "Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=4.0.removed for privacy, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.IdentityModel.Tokens.Jwt, Version=4.0.removed for privacy". Of course, packages already installed. I was trying in a separated new project in an existent solution. Lots of incompatibilities when installing SDK from Nuget. Now testing in brand new solution to make it work and test it as a POC.

     

    Regards,

    Danfer.

    0
    Comment actions Permalink
  • dhabed

    Now I'm able to get token with App user, even able to upload a file. Nevertheless, I can't see the uploaded file (on root, or folder with Id="0") neither other users can see the file. In addition, if running following code, looking for folder content, it only shows the uploaded file, in the object details pop up at VS. Any idea why not being able to see other objects (folders)?

     

     var folderContent = userClient.FoldersManager.GetInformationAsync("0").Result.ItemCollection.Entries;
    
                    BoxFile newFile;
    
                    using (var fileStream = new MemoryStream(fileBytes))
                    {
                        BoxFileRequest req = new BoxFileRequest()
                        {
                            Name = "test.Docx",
                            Parent = new BoxRequestEntity() { Id = "0" },
    
                        };
                        newFile = await userClient.FilesManager.UploadAsync(req, fileStream);
                    }
    0
    Comment actions Permalink
  • kendomen

    I ran the code

     

    string localFilePath = "C:\\dev\\BoxSDKNet\\CreateAppUser\\test.txt";
                string fileName = "test.txt";
                
    
                var file = File.OpenRead(localFilePath);
                var fileRequest = new BoxFileRequest
                {
                    Name = fileName,
                    Parent = new BoxFolderRequest { Id = "0" }
                };
    
                var bFile = await userClient.FilesManager.UploadAsync(fileRequest, file);

    And it shows up in the AppUser's folder

     

    upload.png

    0
    Comment actions Permalink
  • dhabed

    Thanks Kendomen, yes, I could see the file too, eventually. Also, uploading in other folders, the issue was witht the set access of the App user, fixed and now can see the file.

     

    Cheers,

    Danfer.

    0
    Comment actions Permalink
  • dhabed

    Uploadng file complete code using .Net SDK, including the instructions where I was having issues, case anybody find it useful

     public async Task UploadFileAsync(string clientId, string clientSecret, string enterpriseId, string privateKeyPath, string privateKeyPassword,
               string publicKeyId, string appUserId, byte[] fileBytes, string fileName)
            {
                var privateKey = File.ReadAllText(privateKeyPath);
    
                var boxConfig = new BoxConfig(clientId, clientSecret, enterpriseId, privateKey, privateKeyPassword, publicKeyId);
                var boxJWT = new BoxJWTAuth(boxConfig);
    
                var userToken = boxJWT.UserToken(appUserId);
                var userClient = boxJWT.UserClient(userToken, appUserId);
    
                try
                {
                    using (var fileStream = new MemoryStream(fileBytes))
                    {
                        BoxFileRequest req = new BoxFileRequest()
                        {
                            Name = fileName,
                            Parent = new BoxRequestEntity() { Id = "removed for privacy" }
    
                        };
                        var boxFile = await userClient.FilesManager.UploadAsync(req, fileStream);
    
                        var sharedLinkReq = new BoxSharedLinkRequest()
                        {
                            Access = BoxSharedLinkAccessType.open,
                            Permissions = new BoxPermissionsRequest
                            {
                                Download = true,
                                Preview = BoxPermissionType.Open,
                            }
                        };
                        var fileLink = await userClient.FilesManager.CreateSharedLinkAsync(boxFile.Id, sharedLinkReq);
                        var shareLinkUrl = fileLink.SharedLink.Url;
                        var shareLinkDownloadUrl = fileLink.SharedLink.DownloadUrl;
                    }
                }
                catch (Exception ex)
                {
                    //Some exception management
                }
    
            }

     

    0
    Comment actions Permalink

Please sign in to leave a comment.