OneDrive
Note
It’s required to install email
from Optional dependencies section.
Note
Permission |
Application |
Delegated |
Can be limited |
---|---|---|---|
Files.Read.All Read access only
|
✅ |
✅ |
❌ |
Files.ReadWrite.All Read and write access
|
✅ |
✅ |
❌ |
Sites.Read.All Access Sharepoint - Read only
|
✅ |
✅ |
❌ |
Sites.ReadWrite.All Access Sharepoint - Read and Write
|
✅ |
✅ |
❌ |
Sites.Selected Access Sharepoint - Configurable Read / Write
|
✅ |
❌ |
✅ |
User.Read If you need to get user drive id
|
✅ |
✅ |
❌ |
Warning
Important, Application permissions allow access to all drives or sites.
If limiting access is a priority we suggest only using Application permissions with Sites.Selected
which enables limitations to Sharepoint drives.
Otherwise you need to use Delegated permissions for a user with access to the resources needed. But this is not suitable for business critical applications as the user token can expire and an user must approve the application.
Manager
- class aiviro.modules.onedrive.OneDriveManager(auth: BaseAuthentication)
Manages OneDrive API requests.
- Parameters:
auth – Authentication object to use for requests
- Example:
>>> from aiviro.modules.onedrive import OneDriveManager, OneDriveScope >>> manager = OneDriveManager.oauth_public( ... "username", ... "client_id", ... "authority_url", ... main_scope=OneDriveScope.FILES_READWRITE ... ) >>> r_folder = manager.root >>> print(r_folder.name) >>> for child in r_folder.children: ... print(child.name)
- classmethod oauth_public(username: str, client_id: str, authority_url: str, alias: str | None = None, main_scope: ~aiviro.modules.onedrive.common.OneDriveScope = <OneDriveScope.FILES_READWRITE: 2>, additional_scopes: ~typing.Tuple[str, ...] = ()) OneDriveManager
Creates a OneDriveManager instance using a public authentication.
- Parameters:
username – Username to use for authentication
client_id – Client ID to use for authentication
authority_url – Authority URL to use for authentication
alias – Alias to use for authentication
main_scope – Main scope to use for authentication
additional_scopes – Additional scopes to use for authentication
- Returns:
OneDriveManager instance
- classmethod oauth_confidential(username: str, client_id: str, authority_url: str, secret: str, main_scope: ~aiviro.modules.onedrive.common.OneDriveScope = <OneDriveScope.FILES_READWRITE: 2>, additional_scopes: ~typing.Tuple[str, ...] = ()) OneDriveManager
Creates a OneDriveManager instance using a confidential authentication.
- Parameters:
username – Username to use for authentication
client_id – Client ID to use for authentication
authority_url – Authority URL to use for authentication
secret – Secret to use for authentication
main_scope – Main scope to use for authentication
additional_scopes – Additional scopes to use for authentication
- Returns:
OneDriveManager instance
- property default_drive: BaseDrive
Default drive to use for requests.
- property request: MSALRequests
Request handler to use for requests.
- property root: OneDriveFolder
Returns the root folder of the default drive.
- get_item_by_id(idx: str, drive: BaseDrive | None = None) OneDriveFile | OneDriveFolder
Returns the file or folder with the given ID.
- Parameters:
idx – ID of the file or folder
drive – Drive to use for the request
- list_by_id(idx: str, drive: BaseDrive | None = None) Iterable[OneDriveFile | OneDriveFolder]
Lists all items in file or folder with the given ID.
- Parameters:
idx – ID of the file or folder
drive – Drive to use for the request
- get_content_of_id(idx: str, drive: BaseDrive | None = None) bytes
Returns the content of file or folder with the given ID.
- Parameters:
idx – ID of the file or folder
drive – Drive to use for the request
- Returns:
Content of the file or folder
- update_content_of_id(idx: str, content: BinaryIO, drive: BaseDrive | None = None) None
Updates the content of file or folder with the given ID.
- Parameters:
idx – ID of the file or folder
content – New content of the file or folder
drive – Drive to use for the request
- upload_new_content(parent_idx: str, filename: str, content: BinaryIO, drive: BaseDrive | None = None) None
Uploads a new file to the given folder.
- Parameters:
parent_idx – ID of the parent folder
filename – Name of the file
content – Content of the file
drive – Drive to use for the request
- move_item(item_id: str, new_parent_id: str, drive: BaseDrive | None = None) None
Moves an item to a new parent folder.
- Parameters:
item_id – ID of the item to move
new_parent_id – ID of the new parent folder
drive – Drive to use for the request
Files and Folders
- class aiviro.modules.onedrive.manager.OneDriveFile(_manager: OneDriveManager, _model: FileItem, _drive: BaseDrive)
Class for representing file in OneDrive.
- download(destination: str | Path = '', **kwargs: Any) None
Download file to destination.
- Parameters:
destination – Destination path. If not specified, file will be downloaded to current directory.
- update_from_file(path: Path | str) None
Update content of file from file.
- Parameters:
path – Path to file.
- class aiviro.modules.onedrive.manager.OneDriveFolder(_manager: OneDriveManager, _model: FolderItem, _drive: BaseDrive)
Class for representing folder in OneDrive.
- property children: Iterable[OneDriveFile | OneDriveFolder]
Get children of folder.
- download(destination: str | Path = '', include_source_dir: bool = False, max_depth: int | None = None, *, depth: int = 0, **kwargs: Any) None
Download folder to destination.
- Parameters:
destination – Destination path. If not specified, folder will be downloaded to current directory.
include_source_dir – If True, source directory will be included in destination path.
max_depth – Maximum depth of download. If None, all files will be downloaded.
depth – Current depth of download.
- upload(file_name: str, new_file_content: bytes) None
Upload new file to folder.
- Parameters:
file_name – Name of new file.
new_file_content – Content of new file.
- upload_file(file_path: Path | str) None
Upload new file to folder from file.
- Parameters:
file_path – Path to file.