box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5
https://developer.box.com
Apache License 2.0
186 stars 163 forks source link

Metadata query results in invalid scope error #950

Closed pixelnix closed 5 months ago

pixelnix commented 6 months ago

Description of the Issue

Short version: I can fetch items' metadata template, but querying that metadata template results in "scope is invalid" exception.

Trying to use MetadataManager.ExecuteMetadataQueryAsync() to query for items by one of the properties of a metadata template.

When I retrieve items using FolderManager or SearchManager, say, and then retrieve their metadata using MetadataManager.GetAllFileMetadataTemplatesAsync(item.Id), I can see a metadata template assigned to every item I get back. There is a property "$scope" with a value of let's say "enterprise_123456", and a property "$template" with "myMetadataTemplate".

This template has a property "uploadDate" which I would like to query. So I try something like

boxClient.MetadataManager.ExecuteMetadataQueryAsync(new BoxMetadataQueryRequest()
{
    From = "enterprise_123456.myMetadataTemplate",
    AncestorFolderId = "0",
    Marker = searchMarker,
    Limit = 200,
    Query = "uploadDate >= :start and uploadDate < :end",
    QueryParameters = queryParams, // a dictionary with start and end keys
    AutoPaginate = true,
    OrderBy = new List<BoxMetadataQueryOrderBy> { new BoxMetadataQueryOrderBy() { FieldKey = "uploadDate", Direction = BoxSortDirection.ASC } }
});

The result is an exception with the message: invalid_query - Scope is invalid: [enterprise_123456.myMetadataTemplate]

Why can I fetch this metadata template on a per-item basis, but not query it?

mwwoda commented 6 months ago

I see that $template returned from the GetAllFileMetadataTemplatesAsync is in lowercase even if it's defined differently on the Box side (e.g with camelCase). I had same issue when I passed the original template name to the ExecuteMetadataQueryAsync, but converting it to lowercase fixed it. Could you try it on your side?

You can also try using cURL to make the request. See - https://developer.box.com/reference/post-metadata-queries-execute-read/. Se we can sure that SDK is not causing the problem.

pixelnix commented 6 months ago

I tried forcing the template name to lower-case when calling ExecuteMetadataQueryAsync, with no change in results -- still getting the same invalid scope error.

I also don't see what you describe re: the data returned from fetching metadata templates for a given item. The template string value in that payload is definitely camelCased, not lowercased.

I will see if I can figure out how to extract our authentication to a place where I can use cURL to replicate this.

pixelnix commented 6 months ago

It looks like I would have to write a ton of code to try and set up authentication using my private key to generate a JWT, in order to try hitting the API directly either with cURL or directly in .NET with HttpClient or something. Unfortunately I don't have the bandwidth to dive that deep into a totally different testing setup.

Are there any other resources that can tell me what mechanism(s) might prevent my authenticated client from being allowed to query this template? I haven't been able to find any.

congminh1254 commented 6 months ago

Hi @pixelnix

I think that you have something wrong with your request, it can be missing some scope or some parameters is not correct.

I can suggest that you can go through this instruction here to see if it can help anything. In case the issue still persist, kindly send the logs and your sample code to Box Support, with the request ID, and the date time of request, so we can check what is wrong from the server side.

Bests, Minh

stale[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] commented 5 months ago

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box .NET SDK and feel free to open another PR/issue at any time.