Viewing a folder or upload a file to a folder as the co-owner
AnsweredHey all! I am new to the Box API. Currently, our organization has a Box account (which we will call "Account A") that has created a folder (we will call it "Folder A") and shared it with another Box account (which we will call "Account B").
"Account A" is listed as the "owner" of the folder, and "Account B" has "co-owner" permissions. I have created an app using "Account B". I am using the .NET Box SDK with C# and VS 2019. Right now I just want to upload files to "Folder A" using the app I created with "Account B".
I have successfully been able to upload files to Account B's Box drive, but I am unable to see "Folder A" when I request a list of items. Anyone have any suggestions as to how I could do this?
Here is my code:
var config = new BoxConfig(client_id, client_secret, enterprise_id, private_key, private_key_password, public_key_id);
var jwt = new BoxJWTAuth(config);
var admin_token = jwt.AdminToken();
var client = jwt.AdminClient(admin_token, asUser: account_b_user_id);
//Test uploading a file to root folder of Account B. THIS PART WORKS.
BoxFile new_file;
using (FileStream stream = new FileStream("test.txt", FileMode.Open))
{
BoxFileRequest request = new BoxFileRequest()
{
Name = "test.txt",
Parent = new BoxRequestEntity() { Id = "0" }
};
new_file = await client.FilesManager.UploadAsync(request, stream);
}
//Get a list of all items that are in the root folder.
//When I run this line of code, the only thing that shows up is
//the file that I just uploaded in the code above. The folder that
//was shared with Account B (and which Account B is a co-owner of)
//does not show up in the list of items. Where is it????
var items = await client.FoldersManager.GetFolderItemsAsync("0", 500);
-
I figured out my issue. It looks like I needed to authenticate with a user token instead of an admin token. The information was at the following link: https://developer.box.com/en/guides/authentication/jwt/user-access-tokens/#user-access-tokens-with-sdks
So I changed the following lines:
var jwt = new BoxJWTAuth(config);
var admin_token = jwt.AdminToken();
var client = jwt.AdminClient(admin_token, asUser: account_b_user_id);To read like this:
var jwt = new BoxJWTAuth(config);
var token = jwt.UserToken(account_b_user_id);
var client = jwt.UserClient(token, account_b_user_id);After doing that, it worked!
Please sign in to leave a comment.
Comments
1 comment