Python API
MSGraph
- class RPA.MSGraph.MSGraph(client_id: str | None = None, client_secret: str | None = None, token: Token | None = None, refresh_token: str | None = None, redirect_uri: str | None = None, vault_backend: bool = False, vault_secret: str | None = None, file_backend_path: Path | None = WindowsPath('/temp'))
The MSGraph library wraps the O365 package, giving robots the ability to access the Microsoft Graph API programmatically.
OAuth Configuration
Graph’s API primarily authenticates via the OAuth 2.0 authorization code grant flow or OpenID Connect. This library exposes the OAuth 2.0 flow for robots to authenticate on behalf of users. A user must complete an initial authentication flow with the help of our OAuth Graph Example Bot.
For best results, register an app in Azure AD and configure it as so:
The type is “Web App”.
Redirect URI should be
https://login.microsoftonline.com/common/oauth2/nativeclientThe app should be a multi-tenant app.
Accounts in any organizational directoryis checked.Has relevant permissions enabled, check the Microsoft Graph permissions reference for a list of permissions available to MS Graph apps.
- ROBOT_LIBRARY_DOC_FORMAT = 'REST'
- ROBOT_LIBRARY_SCOPE = 'GLOBAL'
- authorize_and_get_token(authorization_url: str) str
Exchanges the OAuth authorization URL obtained from
Generate OAuth Authorization URLfor an access token. This library maintains the user access token for current requests and returns the refresh token to be stored in a secure location (e.g., the Robocorp Control Room Vault).- Parameters:
authorization_url – The full URL retrieved by the user after completing authorization to Microsoft.
- Returns:
A refresh token which should be stored securely for future sessions.
- configure_msgraph_client(client_id: str, client_secret: str, refresh_token: str | None = None, redirect_uri: str | None = None) str | None
Configures the MS Graph client. If a refresh token is known, it can be provided to obtain a current user token to authenticate with. A new refresh token is returned if one is provided.
- Parameters:
client_id – Application client ID.
client_secret – Application client secret.
refresh_token – Authorization refresh token obtained from prior OAuth flows.
redirect_uri – Must be provided with client ID and client secret if not using the default.
- Returns:
A new refresh token if one was provided, or
None.
Creates a sharepoint list and returns the instance.
- Parameters:
list_data – Dictionary with the data for the new list.
site – Site instance obtained from
Get Sharepoint Site.
- Returns:
SharePoint List that was created.
SharepointListobjects have additional properties that can be accessed with dot-notation, see examples below.
- download_file_from_onedrive(target_file: File | str, to_path: str | Path | None = None, name: str | None = None, resource: str | None = None, drive: Drive | str | None = None) Path
Downloads a file from Onedrive.
The downloaded file will be saved to a local path.
- Parameters:
target_file –
DriveItemor file path of the desired file.to_path – Destination folder of the downloaded file, defaults to the current directory.
name – New name for the downloaded file, with or without extension.
resource – Name of the resource if not using default.
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.
- Returns:
Path to the downloaded file.
Downloads file from the share link.
The downloaded file will be saved to a local path.
- Parameters:
share_url – URL of the shared file
to_path – Destination folder of the downloaded file, defaults to the current directory.
name – New name for the downloaded file, with or without extension.
- Returns:
The Path to the downloaded file.
Downloads file from SharePoint.
The downloaded file will be saved to a local folder.
- Parameters:
target_file –
DriveItemor file path of the desired file.site – Site instance obtained from
Get Sharepoint Site.to_path – Destination folder of the downloaded file, defaults to the current directory.
name – New name for the downloaded file, with or without extension.
drive – A
Driveobject or a Drive ID as a string.
- Returns:
Path to the downloaded file.
- download_folder_from_onedrive(target_folder: Folder | str, to_path: str | Path | None = None, resource: str | None = None, drive: Drive | str | None = None) Path
Downloads a folder from OneDrive with all of its contents, including subfolders.
Caution when downloading big folder structures. The downloaded folder will be saved to a local path.
- Parameters:
target_folder –
DriveItemor path of the desired folder.to_path – Destination folder where the download will be saved to, defaults to the current directory.
resource – Name of the resource if not using default.
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.
- Returns:
Path to the downloaded folder.
- find_onedrive_file(search_string: str, target_folder: Folder | str | None = None, include_folders: bool | None = False, resource: str | None = None, drive: Drive | str | None = None) List[DriveItem]
Returns a list of files found in OneDrive based on the search string. If a folder is not specified, the search is done in the entire drive and may include items that were shared with the user. It is possible to pass
rootas the target folder in order to avoid this behavior.The files returned are
DriveItemobjects and they have additional properties that can be accessed with dot-notation, seeList Files In Onedrive Folderfor details.- Parameters:
search_string – String used to search for file in Onedrive. Values may be matched across several fields including filename, metadata, and file content.
target_folder – Folder where to search for files.
include_folders – Boolean indicating if should return folders as well.
resource – Name of the resource if not using default.
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.
- Returns:
List of DriveItems found based on the search string.
- generate_oauth_authorization_url(client_id: str | None = None, client_secret: str | None = None, redirect_uri: str | None = None, scopes: list | None = None) str
Generates an authorization URL which must be opened by the user to complete the OAuth flow. If no scopes are provided, the default scope is used which is all scopes defined in the
Get Scopeskeyword.- Parameters:
client_id – Application client ID.
client_secret – Application client secret.
redirect_uri – Can be provided with client ID and client secret if not using the default.
scopes – A list of scopes in the form required by the API. Use the helper function
Get scopes.
- Returns:
The URL the user must follow to complete their portion of the OAuth flow.
- get_drive_instance(resource: str | None = None, drive_id: str | None = None) Drive
Returns the specified drive from the specified resource. If either is not provided, defaults are returned.
- Parameters:
resource – Name of the resource if not using default.
drive_id – The Drive ID as a string.
- Returns:
The requested
Driveobject.
- get_file_instance(drive: Drive | str, file: File | str) File
Returns a
Fileobject from the provided drive. If aFileobject is provided, it is reobtained from the API.- Parameters:
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.file_path – A
Fileobject or path as a string.
- get_folder_instance(drive: Drive | str | None = None, folder: Folder | str | None = None) Folder
Returns a folder object from the provided drive. If a folder object is provided, it is reobtained from the API.
- Parameters:
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.folder – A
Folderobject or folder path as a string.
Returns the items on a SharePoint list as a table. The list is found by it’s display name.
- Parameters:
list_name – Display name of the SharePoint list.
site – Site instance obtained from
Get Sharepoint Site.
- Returns:
Table or list of dicts of the items.
- get_me() User
Returns the MS Graph object representing the currently logged in user. A
Userobject is returned. Properties of the user can be accessed like so:
- get_scopes(*scopes: str) list
Returns the proper scope definitions based on the provided “scope helpers”, which are enumerated below. You can pass none to get all scopes. Basic is included in all other scopes. The provided object can be passed to the
scopesparameter when callingGenerate OAuth Authorization URL.basicmailboxmailbox_sharedmessage_sendmessage_send_sharedmessage_allmessage_all_sharedaddress_bookaddress_book_sharedaddress_book_alladdress_book_all_sharedcalendarcalendar_sharedcalendar_allcalendar_shared_allusersonedriveonedrive_allsharepointsharepoint_dlsettings_alltaskstasks_allpresence
- Parameters:
scopes – Scopes requested. If left empty, all scopes will be returned.
- Returns:
A list of Scopes which can be passed to authorization keywords.
Returns a SharePoint site.
- Parameters:
args – The SharePoint site to retrieve, see documentation for different methods of defining the site.
resource – Name of the resource if not using default.
- Returns:
SharePoint Site instance.
There are multiple ways of retrieving a SharePoint site:
The return is of type Site and it has additional properties that can be accessed with dot-notation. See examples below.
- list_files_in_onedrive_folder(target_folder: Folder | str | None = None, include_folders: bool | None = False, resource: str | None = None, drive: Drive | str | None = None) List[DriveItem]
Returns a list of files from the specified OneDrive folder.
The files returned are
DriveItemobjects and they have additional properties that can be accessed with dot-notation.- Parameters:
target_folder – Path of the folder in Onedrive. If none is provided, will return files from the root of Onedrive.
include_folders – Boolean indicating if should return folders as well.
resource – Name of the resource if not using default.
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.
- Returns:
List of DriveItems in the folder.
List files in the SharePoint Site drive.
If the
driveis not set, the default Document Library will be used. Thedrivecan be obtained from the keywordList Sharepoint Site Drives.The files returned are
DriveItemobjects and they have additional properties that can be accessed with dot-notation, seeList Files In Onedrive Folderfor details.- Parameters:
site – Site instance obtained from
Get Sharepoint Site.include_folders – Boolean indicating if should return folders as well.
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.target_folder – Path of the folder in the Sharepoint drive. If none is provided, will return files from the root of the drive.
- Returns:
List of DriveItems present in the Site drive.
Get a list of Drives available in the SharePoint Site.
- Parameters:
site – Site instance obtained from
Get Sharepoint Site.- Returns:
List of Drives present in the SharePoint Site.
- refresh_oauth_token(refresh_token: str | None = None) str
Refreshes the user token using the provided
refresh_token. The user token is retained in the library and a new refresh token is returned. If no token is provided, this keyword assumes the Robocorp Vault is being used as a backend and attempts to refresh it based on that backend.- Parameters:
refresh_token – The refresh token to use to refresh the session.
- Returns:
A refresh token which should be stored securely for future sessions.
- search_for_users(search_string: str, search_field: str = 'displayName', resource: str = 'users') List[User]
Returns a list of
Userobjects from the Active Directory based on the provided search string.Userobjects have additional properties that can be accessed with dot-notation, seeGet Mefor additional details.- Parameters:
search_string – Text to search for.
search_field – Where to search. Defaults to display name.
resource – Name of the resource if not using default.
- upload_file_to_onedrive(file_path: str, target_folder: Folder | str | None = None, resource: str | None = None, drive: Drive | str | None = None) DriveItem
Uploads a file to the specified OneDrive folder.
The uploaded file is returned as a
DriveItemobject and it has additional properties that can be accessed with dot-notation, seeList Files In Onedrive Folderfor details.- Parameters:
file_path – Path of the local file being uploaded.
target_folder – Path of the folder in Onedrive.
resource – Name of the resource if not using default.
drive – A
Driveobject or Drive ID as a string. If not provided, will use the default drive.
- Returns:
The newly created DriveItem.