Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The API returned an error [NotFound | qxnb0bg5iiq5y2ly.0i3diclct8jjvinf2psftl970gr] not_found - Not Found"} System.Exception {Box.V2.Exceptions.BoxException} #579

Closed
AdilKamal44 opened this issue Jul 30, 2019 · 11 comments

Comments

@AdilKamal44
Copy link

I am using the code below and getting the exception above.
Here is my code.

static readonly string rediredUri = ConfigurationManager.AppSettings["rediredUri"];
Uri uri = new Uri(rediredUri);
string apptoken = ConfigurationManager.AppSettings["Apptoken"];
var config = new BoxConfig(CLIENT_ID, "", uri);
var session = new BoxJWTAuth(config);
var client = session.AdminClient(apptoken);
var folderId = ConfigurationManager.AppSettings["FolderID"];
var folder = await client.FoldersManager.GetInformationAsync(folderId);

@mattwiller
Copy link
Contributor

@AdilKamal44 That error likely means that the user who you're authenticating the SDK as does not have access to the folder. I'm not sure sure you're generating the token you're plugging into session.AdminClient(), but you'll need to make sure that whichever user or service account is authenticated actually has access to the folder in question.

@closedstack
Copy link

closedstack commented Jul 31, 2019

@AdilKamal44 Are you using a developer Token or JWT Auth (since I don't see the json file I suppose it is dev token - which expires in 60 minutes). If you are using JWT, the behavior for adminclient is a bit more restrictive than using the developer token... what permissions does the App and User owning the app have? Try using the user client instead of the admin client.

I use powershell.. but since it is .Net Classes.. maybe this will help

# Import the JSON file 
$json = Get-Content $credFile

#Create a Box instance
$iBoxConfig = [Box.V2.Config.BoxConfig]::CreateFromJsonString($json)
$JWTAuth = [Box.V2.JWTAuth.BoxJWTAuth]::new($iBoxConfig)
#Set the Authentication
Write-DebugLog -file $logfile -Message "Getting the Admin Token"
$adminToken = $JWTAuth.AdminToken()
$adminclient = $JWTAuth.AdminClient($adminToken)
#$adminclient.UsersManager.GetCurrentUserInformationAsync().GetAwaiter().GetResult()
#Need a User Client to upload to user folder and not to the Automation user context with ID
#This does not have to have admin permissions on the entire instance
#Definitely needs "Generate User Access Tokens" Permission, which is linked to "Manage Users"
#Read-Write Permissions as needed. Tested that it can't access other user accounts, but is able to list other users
$userid = <myUserIdInt>
Write-DebugLog -file $logfile -Message "Getting user token"
$ut = $adminclient.Auth.BoxJWTAuth.UserToken($userid)
$uc = $adminclient.Auth.BoxJWTAuth.UserClient($ut,$userid) 

$pagesize = 500
$offset = 0
$folderId = 0
$myFolder = $uc.FoldersManager.GetFolderItemsAsync($folderID,$pagesize,$offset).GetAwaiter().GetResult().entries | 
    select -Property Name, Type, SequenceID, ETag, ID | where {$_.Type -eq 'folder' -and $_.Name -eq 'MyDocs'}
$files = $uc.FoldersManager.GetFolderItemsAsync($myFolder.Id,$pagesize,$offset).GetAwaiter().GetResult().Entries | where {$_.Type -eq 'file'}
$fcount = ($files | Measure-Object).Count
Write-DebugLog -file $logfile -Message "There are $fcount files in folder"

If you are using a developer token, it is fairly simple to use with Rest API and test it with Postman to see if the REST API has the same error (permissions issue)

$offset=0
$limit=1000
#Need to get a fresh token manually from App Configuration every 60 minutes
$bearerToken = "xXxxxxxxxxxxxxxxxxxxxxxxxxxxxxhH"
$getfolderURL = "https://api.box.com/2.0/folders/0"
$getUsersURL = "https://api.box.com/2.0/users"
$authHeader = @{
    Authorization = "Bearer $bearerToken"
}
#Do an initial get of the total user count and upto first 1000 users
$folders = Invoke-RestMethod -Uri $getfolderURL -Headers $authHeader -Method Get

@AdilKamal44
Copy link
Author

@AdilKamal44 Are you using a developer Token or JWT Auth (since I don't see the json file I suppose it is dev token - which expires in 60 minutes). If you are using JWT, the behavior for adminclient is a bit more restrictive than using the developer token... what permissions does the App and User owning the app have? Try using the user client instead of the admin client.

I use powershell.. but since it is .Net Classes.. maybe this will help

# Import the JSON file 
$json = Get-Content $credFile

#Create a Box instance
$iBoxConfig = [Box.V2.Config.BoxConfig]::CreateFromJsonString($json)
$JWTAuth = [Box.V2.JWTAuth.BoxJWTAuth]::new($iBoxConfig)
#Set the Authentication
Write-DebugLog -file $logfile -Message "Getting the Admin Token"
$adminToken = $JWTAuth.AdminToken()
$adminclient = $JWTAuth.AdminClient($adminToken)
#$adminclient.UsersManager.GetCurrentUserInformationAsync().GetAwaiter().GetResult()
#Need a User Client to upload to user folder and not to the Automation user context with ID
#This does not have to have admin permissions on the entire instance
#Definitely needs "Generate User Access Tokens" Permission, which is linked to "Manage Users"
#Read-Write Permissions as needed. Tested that it can't access other user accounts, but is able to list other users
$userid = <myUserIdInt>
Write-DebugLog -file $logfile -Message "Getting user token"
$ut = $adminclient.Auth.BoxJWTAuth.UserToken($userid)
$uc = $adminclient.Auth.BoxJWTAuth.UserClient($ut,$userid) 

$pagesize = 500
$offset = 0
$folderId = 0
$myFolder = $uc.FoldersManager.GetFolderItemsAsync($folderID,$pagesize,$offset).GetAwaiter().GetResult().entries | 
    select -Property Name, Type, SequenceID, ETag, ID | where {$_.Type -eq 'folder' -and $_.Name -eq 'MyDocs'}
$files = $uc.FoldersManager.GetFolderItemsAsync($myFolder.Id,$pagesize,$offset).GetAwaiter().GetResult().Entries | where {$_.Type -eq 'file'}
$fcount = ($files | Measure-Object).Count
Write-DebugLog -file $logfile -Message "There are $fcount files in folder"

If you are using a developer token, it is fairly simple to use with Rest API and test it with Postman to see if the REST API has the same error (permissions issue)

$offset=0
$limit=1000
#Need to get a fresh token manually from App Configuration every 60 minutes
$bearerToken = "xXxxxxxxxxxxxxxxxxxxxxxxxxxxxxhH"
$getfolderURL = "https://api.box.com/2.0/folders/0"
$getUsersURL = "https://api.box.com/2.0/users"
$authHeader = @{
    Authorization = "Bearer $bearerToken"
}
#Do an initial get of the total user count and upto first 1000 users
$folders = Invoke-RestMethod -Uri $getfolderURL -Headers $authHeader -Method Get

Hi Sunil,
This is the first time I am using this Box SDK and I don't know how to generate config file, could you please guide me how do you generate config file?

@AdilKamal44
Copy link
Author

@AdilKamal44 That error likely means that the user who you're authenticating the SDK as does not have access to the folder. I'm not sure sure you're generating the token you're plugging into session.AdminClient(), but you'll need to make sure that whichever user or service account is authenticated actually has access to the folder in question.

Hi Matt,
If I pass the folderID as "0" I am getting the folder as "All Files" but under that folder there is another folder from where I want to download the file. That folder doesn't display show under the "All Files" folder. Is there any specific permission I have to give from the App? The only option I see is "Share". Could you please guide me how to give permissions on that folder?

@mattwiller
Copy link
Contributor

@AdilKamal44 If you don't see the folders you expect to under All Files, chances are that the SDK is authenticated as a different user than you expect. You can confirm this by getting the current user information:

BoxUser currentUser = await client.UsersManager.GetCurrentUserInformationAsync();
Debug.WriteLine($"The current user login is {currentUser.Login}");

It's possible that the account is a Service Account, which has its own folders and files separate from your personal account. If you want the Service Account to have access to a folder from your own account, you should be able to collaborate it to that folder just like you would any other user.

@AdilKamal44
Copy link
Author

@AdilKamal44 If you don't see the folders you expect to under All Files, chances are that the SDK is authenticated as a different user than you expect. You can confirm this by getting the current user information:

BoxUser currentUser = await client.UsersManager.GetCurrentUserInformationAsync();
Debug.WriteLine($"The current user login is {currentUser.Login}");

It's possible that the account is a Service Account, which has its own folders and files separate from your personal account. If you want the Service Account to have access to a folder from your own account, you should be able to collaborate it to that folder just like you would any other user.

Hi Matt,
Thanks for the reply, I got the user as AutomationUser which is not user I logged in as, how do I give him access to the folders?

@mattwiller
Copy link
Contributor

@AdilKamal44 As I mentioned above, if you want the Service Account to have access to a folder from your own account, you should be able to collaborate it to that folder just like you would any other user. Since you have the login for that user, you should be able to just add it to whichever folder you want.

@AdilKamal44
Copy link
Author

@AdilKamal44 As I mentioned above, if you want the Service Account to have access to a folder from your own account, you should be able to collaborate it to that folder just like you would any other user. Since you have the login for that user, you should be able to just add it to whichever folder you want.

Hi Matt,
This is the first time I am using this application and doesn't have enough knowledge about the product. You mean i add this user as Collaborator on the folder which I am trying to access? Or I have to share the folder and invite this service account user?

@mattwiller
Copy link
Contributor

@AdilKamal44 You're correct — you should be able to do this from the web application by adding the service account as a collaborator on the folder you want it to have access to. If you need help with that part, please contact Box Support; they can assist you further with topics outside of the SDK.

@AdilKamal44
Copy link
Author

@AdilKamal44 You're correct — you should be able to do this from the web application by adding the service account as a collaborator on the folder you want it to have access to. If you need help with that part, please contact Box Support; they can assist you further with topics outside of the SDK.

Matt,
Thanks alot, I was able to add the user there and able to download the file. I really appreciate your help.

@mattwiller
Copy link
Contributor

@AdilKamal44 Great — I'm glad it's working for you now! Please let us know if you run into any further issues with the SDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants