Module microsoft.onedrive

ballerinax/microsoft.onedrive Ballerina library

3.0.0

Overview

Microsoft OneDrive is a cloud-based file storage service provided by Microsoft, allowing users and organizations to store, share, and manage files securely online.

The ballerinax/microsoft.onedrive package offers APIs to connect and interact with OneDrive API endpoints, specifically based on Microsoft Graph v1.0. This package enables developers to perform operations such as uploading, downloading, and sharing files and folders on OneDrive using the Ballerina language.

Setup guide

Step 1. Register the application

  1. Sign in to the Microsoft Entra admin center.

  2. Go to App registrations > New registration.

    New Registration

  3. Enter a display name for your application.

    Register Application

  4. Select who can use the application in the Supported account types section.

  5. Leave Redirect URI blank for now (you will configure it later).

  6. Click Register to complete the initial app registration.

  7. After registration, note the Application (client) ID from the Overview pane.

    Application Overview

Step 2. Configure platform settings

  1. Under Manage, select Authentication.

  2. Under Platform configurations, click Add a platform.

  3. Select the Web tile.

  4. Set the Redirect URI to http://localhost.

    Configure Web

  5. Click Configure to save the platform configuration.

Step 3. Add credentials

  1. Go to Certificates & secrets > Client secrets > New client secret.

    Add Secret

  2. Add a description for your client secret.

    Add Description

  3. Select an expiration period or specify a custom lifetime.

  4. Click Add.

  5. Copy and save the secret value for use in your client application code. You will not be able to view it again after leaving the page.

    Save Secret

Step 4. Get the Auth Tokens

  1. In your browser, enter the following URL (replace <client-id> with your actual client ID):

    https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=<client-id>&scope=offline_access files.read files.read.all files.readwrite files.readwrite.all&response_type=code&redirect_uri=http://localhost
    

    Query parameter details:

    ParameterDescription
    client_idThe Application (client) ID from your Azure app registration.
    scopeThe permissions your app is requesting. For OneDrive, these include:
    - offline_access: Allows your app to receive refresh tokens for long-lived access.
    - files.read: Read files the user can access.
    - files.read.all: Read all files the user can access, including those shared with them.
    - files.readwrite: Read and write files the user can access.
    - files.readwrite.all: Read and write all files the user can access, including those shared with them.
    response_typeSet to code to request an authorization code for OAuth2.
    redirect_uriThe URI to redirect to after authentication. Must match the URI configured in your Azure app registration (e.g., http://localhost).
  2. Grant access to the application and click Accept.

    Give Access

  3. After authentication, you will be redirected to a URL like:

    http://localhost/?code=<auth-code>
    

    Copy the authorization code from the URL.

  4. Exchange the authorization code for access and refresh tokens by sending the following request:

    curl -X POST https://login.microsoftonline.com/common/oauth2/v2.0/token \
      -H "Content-Type: application/x-www-form-urlencoded" \
      -d "client_id=<client-id>&client_secret=<client-secret>&redirect_uri=http://localhost&code=<auth-code>&grant_type=authorization_code"
    

    The tokens will be returned in the response.

Quickstart

To use the microsoft.onedrive connector in your Ballerina application, modify the .bal file as follows:

Step 1: Import the module

Import the microsoft.onedrive module.

Copy
import ballerinax/microsoft.onedrive;

Step 2: Instantiate a new connector

Create a onedrive:ConnectionConfig with the obtained OAuth2.0 tokens and initialize the connector with it.

Copy
configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;

onedrive:Client onedrive = check new (
    config = {
        auth: {
            refreshToken,
            clientId,
            clientSecret,
            scopes: ["Files.Read", "Files.Read.All", "Files.ReadWrite", "Files.ReadWrite.All"]
        }
    }
);

Step 3: Invoke the connector operation

Now, utilize the available connector operations.

See all the drives available

Copy
onedrive:DriveCollectionResponse driveItems = check oneDriveClient->listDrive();

Examples

The microsoft.onedrive connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering the following use cases:

  1. Upload File - This example demonstrates how to use the Ballerina Microsoft OneDrive connector to upload a file from your local system to your OneDrive account.

Import

import ballerinax/microsoft.onedrive;Copy

Other versions

See more...

Metadata

Released date: 4 days ago

Version: 3.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 2466

Current verison: 5


Weekly downloads


Source repository


Keywords

Content & Files/File Management & Storage

Cost/Freemium

Vendor/Microsoft


Contributors