Box .NET api
I'm created a simple command-line app in .NET to upload a file to a Box.com folder. There does not seem to be ANY documentation on how to use the .NET API - just a couple of too-simplistic sample apps. The app works fine if I login to the server and run it from a command prompt. When I add the command to a SQL Server stored procedure, I get the following error: Exception creating BoxClient: {"error":"invalid_grant","error_description":"Current date\/time MUST be before the expiration date\/time listed in the 'exp' claim"}
Here is the code I cobbled together from the .NET samples:
static void Main(string[] args) { try { Task t = MainAsync(args); t.Wait(); } catch (Exception ex) { Console.WriteLine(ex.InnerException.Message); } } private static async Task MainAsync(string[] args) { string boxUser = args[0]; string boxPath = args[1]; string filePath = args[2]; IBoxConfig boxConfig; try { boxConfig = new BoxConfig(CLIENT_ID, CLIENT_SECRET, ENTERPRISE_ID, PRIVATE_KEY, JWT_PRIVATE_KEY_PASSWORD, JWT_PUBLIC_KEY_ID); } catch(Exception ex) { if (ex.InnerException != null) throw new Exception("Exception creating BoxConfig: " + ex.InnerException.Message, ex.InnerException); else throw new Exception("Exception creating BoxConfig: " + ex.Message, ex); } BoxJWTAuth boxJWT; try { boxJWT = new BoxJWTAuth(boxConfig); } catch (Exception ex) { if (ex.InnerException != null) throw new Exception("Exception creating BoxJWTAuth: " + ex.InnerException.Message, ex.InnerException); else throw new Exception("Exception creating BoxJWTAuth: " + ex.Message, ex); } string userToken = boxJWT.UserToken(boxUser); BoxClient userClient; try { userClient = boxJWT.UserClient(userToken, boxUser); } catch (Exception ex) { if (ex.InnerException != null) throw new Exception("Exception creating BoxClient: " + ex.InnerException.Message, ex.InnerException); else throw new Exception("Exception creating BoxClient: " + ex.Message, ex); } try { await UploadFile(boxPath, filePath, userClient); } catch (Exception ex) { if (ex.InnerException != null) throw new Exception("Exception Uploading File: " + ex.InnerException.Message, ex.InnerException); else throw new Exception("Exception Uploading File: " + ex.Message, ex); } } private static async Task UploadFile(string boxPath, string filePath, BoxClient userClient) { var boxFolderId = await FindBoxFolderId(userClient, boxPath); using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read)) { // Create request object with name and parent folder the file should be uploaded to BoxFileRequest request = new BoxFileRequest() { Name = Path.GetFileName(filePath), Parent = new BoxRequestEntity() { Id = boxFolderId } }; await userClient.FilesManager.UploadAsync(request, fs); } } private static async Task FindBoxFolderId(BoxClient client, string path) { var folderNames = path.Split('/'); folderNames = folderNames.Where((f) => !String.IsNullOrEmpty(f)).ToArray(); //get rid of leading empty entry in case of leading slash var currFolderId = "0"; //the root folder is always "0" foreach (string folderName in folderNames) { var folderInfo = await client.FoldersManager.GetInformationAsync(currFolderId); var foundFolder = folderInfo.ItemCollection.Entries.OfType().First((f) => f.Name == folderName); currFolderId = foundFolder.Id; } return currFolderId; }
Please sign in to leave a comment.
Comments
1 comment