Azure / Microsoft365R

R SDK for interacting with Microsoft 365 APIs
Other
308 stars 42 forks source link

unable to access nested files within shared folder - onedrive business #141

Closed benhmin closed 1 year ago

benhmin commented 1 year ago

Hi. First Thank you for this great package.

I can connect successfully to my organization's business one drive and I can list the top level items from my own files as well as those shared with me. For my files I can also then go deeper and access child items using either the full path or using the list_items() functions of the top level items.

However, for shared folders I am getting errors indicating that the item is not a folder.

Example: od <- get_business_onedrive() od$get_item_properties('Line Complications')

This will then show that the item is a shared folder with 3 children listed under $remoteItem$folder$childCount However, when I then do: od$get_item('Line Complications')$is_folder() The result is FALSE

This then leads to problems when I attempt to access child items within the folder. For example, od$get_item('Line Complications/Data') gives the error: Error in process_response(res, match.arg(http_status_handler), simplify) : Not Found (HTTP 404). Failed to complete operation. Message: The resource could not be found.

And the following: od$get_item('Line Complications')$list_items() gives the error: Error: attempt to apply non-function and od$get_item('Line Complications')$is_folder() results as FALSE

I suspect that this may be in some way related to the function is_folder() because it seems to look for properties within self$properties$folder$childCount while for shared items the information for child items seems to be stored in self$properties$remoteItem$folder$childCount.

For now this completely prevents me from accessing children within shared folders. Am I missing something about how I should be doing this? Thanks so much.

hongooi73 commented 1 year ago

Unfortunately I don't have access to a business MS365 account right now, but I'll keep track of this.

hongooi73 commented 1 year ago

I have a fix for shared folders in the dev branch; can you try it and see if it fixes this problem?

You can install via install_github("Azure/Microsoft365R@dev")

benhmin commented 1 year ago

thank you. I will try the dev version out as soon as I get a chance

benhmin commented 1 year ago

I'm seeing the same issues with the dev version from today.

> packageVersion("Microsoft365R") [1] ‘2.3.4.9000’

odb$get_item('Line Complications')$is_folder() [1] FALSE odb$get_item('Line Complications/Data') Error in process_response(res, match.arg(http_status_handler), simplify) : Not Found (HTTP 404). Failed to complete operation. Message: The resource could not be found. odb$get_item('Line Complications')$list_items() Error: This method is only applicable for a folder item

Am I using the correct version of the package?

hongooi73 commented 1 year ago

Whoops, I forgot to mention you have to set a global option:

options(microsoft365r_use_itemid_in_path=TRUE)
benhmin commented 1 year ago

This didn't seem to work for me. I got the same results when I ran the above commands in a new R session after setting the global option options(microsoft365r_use_itemid_in_path=TRUE)

Interestingly, when I even do list_items() on my business onedrive it doesn't show shared folders that have been added to my main folder.

attempting od$download_file("file_path") works fine for files that are mine, but gives the following error if I attempt it for a shared file: Error in process_response(res, match.arg(http_status_handler), simplify) : Not Found (HTTP 404). Failed to complete operation. Message: The resource could not be found.

hongooi73 commented 1 year ago

I'm still in the process of debugging this. It's frustrating because there seems to be an AAD user configuration issue mixed in as well; I can see the shared file if I access it one way, but not another.

hongooi73 commented 1 year ago

@benhmin just to clarify: are you able to download shared files? Ie, is this only an issue with folders?

hongooi73 commented 1 year ago

@benhmin I've pushed a change to master that should hopefully fix this. Can you give it a try?

benhmin commented 1 year ago

Hi @hongooi73 I have installed the most recent version (2.3.4). At this point I can see all the shared files/folders via list_shared_files() and list_shared_items().

However, I appear not to be able to download any shared items or list items within shared folders. I can get properties on shared folders but only if I have chosen "add shortcut to my files" on the web interface for that folder. That option is not available for individual files so I can't get properties on them or download them.

Is there a special path I should be entering when referring to shared files when I try to download them? Can I refer to these files and folders directly by ID in the operations?

hongooi73 commented 1 year ago

Please post the exact code you ran and the error you saw. Also note that you have to install from the repo with install_github() or similar, as I haven't updated the CRAN version yet.

benhmin commented 1 year ago

@hongooi73 Sorry about that. Code and results posted this time. Sorry, I didn't install from github, i misinterpreted your statement that you had pushed to master as published to CRAN.

Anyway:

library(Microsoft365R) od <- get_business_onedrive() od$get_item_properties('Line Complications/Data')

Error in process_response(res, match.arg(http_status_handler), simplify) : Not Found (HTTP 404). Failed to complete operation. Message: The resource could not be found.

However, od$get_item('Line Complications')$get_item('Data')$is_folder()

TRUE

od$get_item('Line Complications')$get_item('Data')$list_items()

                                 name     size isdir                                 id

1 12y lines 11.14.22.xlsx 898422 FALSE 01UAGNWK4SENYBD54FENFYXI6PQ4G52FWX 2 lda_extract - Copy 1.csv 26536504 FALSE 01UAGNWK2HIO23YCVF5VAI63YNFEI77LJ2 3 lda_extract - Copy 1.xlsx 1948994 FALSE 01UAGNWK4XEZU5T7C7IBG2WLXYB3IGIIMU 4 lda_extract.csv 26536504 FALSE 01UAGNWK5ANKYBA45WRBAI5H5TRZPGD567

od$get_item('Line Complications')$get_item('Data')$download_file('lda_extract.csv')

Error: attempt to apply non-function

od$get_item('Line Complications')$get_item('Data')$get_item('lda_extract.csv')$download(overwrite = T)

Is this the expected way to access nested files within a shared folder? it works but a bit convoluted for deeply nested files.

This also seems to work: files <- od$list_shared_files() files$`Line Complications`$download('Data/lda_extract.csv')

also, files <- od$list_shared_files() files$`Another Beacon question.ppt`$download()

Thanks

hongooi73 commented 1 year ago

That isn't exactly expected, but also working properly. While you can't go directly to a subitem in a shared folder, you should be able to do something like this:

od$get_item("Line Complications")$get_item("Data/lda_extract.csv")$download()

basically get the shared item into its own object, then do the usual operations with it. This is because when you do od$download_file("x/y/z") R doesn't know that the top-level folder is shared, so it constructs the request to the subitem as if it was a regular folder. Once you get the folder into an object, then it knows that it's shared and can construct the correct request.

I'll update the docs to point this out.