This project demonstrates how to connect to a OneDrive account and update an Excel file using the Microsoft Graph API. The project includes a Django web application for reading and writing Excel files, as well as for authenticating and accessing OneDrive.
The reading and writing steps require manual authorisation actions. Those steps can be improved by automating them to make the application more user-friendly.
- Python 3.x
- Django
- Required Python libraries:
msal
,requests
-
Clone the repository:
git clone https://github.com/serkosi/cloud-storage-manipulator.git cd cloud-storage-manipulator-main
-
Install the required Python libraries:
pip install django msal requests
-
Set up Microsoft Graph API:
- Register your app in the Azure portal to get the necessary credentials (client ID and secret).
- Add the required API permissions (Files.ReadWrite.All, Sites.ReadWrite.All).
- Create a credentials.json file in the root directory of the project (where
manage.py
is located) and save your credentials and file path:{ "client_id": "your_client_id", "client_secret": "your_client_secret", "tenant_id": "your_tenant_id", "file_path": "your_file_path" # Update this with your file path }
-
Ensure Proper Authentication:
- Register Your Application in Azure:
- Go to the Azure portal.
- Navigate to
Azure Active Directory
>App registrations
>New registration
. - Register your application and note down the
client_id
andtenant_id
.
- Configure API Permissions:
- In the Azure portal, go to your registered application.
- Navigate to
API permissions
>Add a permission
. - Select
Microsoft Graph
>Delegated permissions
. - Add the
Files.ReadWrite.All
permission. - Grant admin consent for the permissions.
- Create a Client Secret:
- In the Azure portal, go to your registered application.
- Navigate to
Certificates & secrets
>New client secret
. - Note down the
client_secret
.
- Register Your Application in Azure:
-
Navigate to the Django template directory:
cd vscode-python-templates/django-template
-
Apply migrations:
python manage.py migrate
-
Run the development server:
python manage.py runserver
-
Access the application: Open your web browser and navigate to
http://localhost:8000
.
-
Read Excel File:
- Navigate to
http://localhost:8000/read_excel/
to read the value of a specific cell in the Excel file. - You will be prompted to visit a URL for authentication. Copy the URL from the terminal, open it in your browser, and authenticate.
- After authentication, you will receive an authorization code. Copy this code and paste it back into the terminal.
- Navigate to
-
Update Excel File:
- Navigate to
http://localhost:8000/update_excel/
to update the value of a specific cell in the Excel file. - You will be prompted to visit a URL for authentication. Copy the URL from the terminal, open it in your browser, and authenticate.
- After authentication, you will receive another authorization code. Copy this code and paste it back into the terminal.
- The process will automatically navigate to the read Excel file with the updated cell value.
- Navigate to
hello/auth.py
: Contains the authentication logic using MSAL.hello/views.py
: Contains the views for reading and updating the Excel file.hello/templates/hello/read_excel.html
: Template for displaying the cell value.hello/templates/hello/update_excel.html
: Template for updating the cell value.
This project is licensed under the MIT License.