Table of Contents

Authentication

TTMSFNCCloudMicrosoftOneDrive talks to Microsoft OneDrive over OAuth 2.0, so every session starts by authenticating the user and obtaining an access token. You register an app with Microsoft, supply its credentials on the Authentication object, and call Connect to run the consent flow; enabling PersistTokens lets the same call reuse or silently refresh a previous session instead of prompting the browser every time. This chapter covers the credential setup, the connect flow, persisting tokens, and reading the connected account's information.

App registration credentials

Register an app in the Microsoft Entra admin center to get a client ID and client secret, and register a redirect URI. Set the matching values on Authentication before connecting:

  • Authentication.ClientID — the app's client ID.
  • Authentication.Secret — the app's client secret.
  • Authentication.CallBackURL — a redirect URI registered on the app (a local address such as http://127.0.0.1:8000 is typical for desktop apps).

Connecting

Connect opens the Microsoft consent page in the browser, then exchanges the returned code for tokens, including a refresh token. Handle OnConnected to know when the component is ready to make requests.

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Credentials from your app registration (https://portal.azure.com).
  TMSFNCCloudMicrosoftOneDrive1.Authentication.ClientID := 'your-client-id';
  TMSFNCCloudMicrosoftOneDrive1.Authentication.Secret := 'your-client-secret';
  // Must match a redirect URI registered on the app.
  TMSFNCCloudMicrosoftOneDrive1.Authentication.CallBackURL := 'http://127.0.0.1:8000';

  TMSFNCCloudMicrosoftOneDrive1.OnConnected := OneDriveConnected;
  // Opens the Microsoft consent page in the browser and exchanges the code for tokens.
  TMSFNCCloudMicrosoftOneDrive1.Connect;
end;

procedure TForm1.OneDriveConnected(Sender: TObject);
begin
  // Tokens (including a refresh token) are now available on Authentication.
  TMSFNCCloudMicrosoftOneDrive1.GetAccountInfo;
end;

Combining Connect with persisted and refreshed tokens

Connect already does more than run the interactive flow: it first loads any previously saved tokens, tests them, and — when the access token has expired but a refresh token is available — silently exchanges it for a new one before falling back to the browser. Setting PersistTokens.Section to a non-empty name turns this on, so a returning user with a still-valid or refreshable session never sees the browser again:

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCCloudMicrosoftOneDrive1.Authentication.ClientID := 'your-client-id';
  TMSFNCCloudMicrosoftOneDrive1.Authentication.Secret := 'your-client-secret';
  TMSFNCCloudMicrosoftOneDrive1.Authentication.CallBackURL := 'http://127.0.0.1:8000';

  // A non-empty Section switches on automatic token persistence: Connect
  // loads any saved AccessToken / AccessTokenRefresh, tests them, silently
  // refreshes an expired access token, and saves the (possibly refreshed)
  // tokens again on success.
  TMSFNCCloudMicrosoftOneDrive1.PersistTokens.Section := 'MyAppOneDrive';

  TMSFNCCloudMicrosoftOneDrive1.OnConnected := OneDriveConnected;
  // Only opens the browser when there is no valid or refreshable session.
  TMSFNCCloudMicrosoftOneDrive1.Connect;
end;

procedure TForm1.OneDriveConnected(Sender: TObject);
begin
  TMSFNCCloudMicrosoftOneDrive1.GetAccountInfo;
end;
Note

PersistTokens writes to local storage in cleartext by default. For a deployed app, point PersistTokens.Location at a location your own encryption or OS keychain integration controls.

Account information

GetAccountInfo requests the connected user's identity into Info, raising OnGetAccountInfo when the result is available — including the account's display name, ID, and Email address.

procedure TForm1.ShowAccountInfo;
begin
  TMSFNCCloudMicrosoftOneDrive1.OnGetAccountInfo := OneDriveAccountInfo;
  TMSFNCCloudMicrosoftOneDrive1.GetAccountInfo;
end;

procedure TForm1.OneDriveAccountInfo(Sender: TObject;
  const AInfo: TTMSFNCCloudMicrosoftOneDriveInfo;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add(AInfo.UserName + ' <' + AInfo.Email + '>');
end;

Common mistakes

  • Mismatched redirect URI. Authentication.CallBackURL must exactly match a redirect URI registered on the app, or the consent flow fails.
  • Calling operations before OnConnected. Requests issued before the tokens are in place fail; start them from OnConnected.
  • Leaving PersistTokens.Section empty. Tokens are silently not persisted, so every Connect call requires the interactive browser flow again.

See also