Table of Contents

Persisting tokens

OAuth-based TMS FNC Cloud Pack components can persist authentication tokens so returning users do not need to approve the same application every time it starts.

Choose a storage location

Configure token storage through the component's PersistTokens property. Use a distinct PersistTokens.Section for each service account so tokens from different providers or accounts do not overwrite one another.

Location Use when Required settings
INI file The application stores credentials in a local settings file. PersistTokens.Location := plIniFile and PersistTokens.Key set to the INI file path.
Database Tokens should be stored in an application dataset. PersistTokens.Location := plDataBase and PersistTokens.DataSource assigned to an active dataset.

For WEB applications, persisted values are stored in browser local storage under the configured section name.

Database fields

Database token storage expects these fields when the corresponding values are available:

Field Purpose
ACCESSTOKEN Current access token.
AUTHENTICATIONTOKEN Authentication token returned during the OAuth flow.
ACCESSTOKENSECRET Access token secret for providers that use it.
AUTHENTICATIONTOKENSECRET Authentication token secret for providers that use it.
ACCESSTOKENREFRESH Refresh token used to renew access.
ACCESSTOKENEXPIRY Expiry timestamp for the current access token.

Optional fields include CLIENTID, KEY, SECRET, CALLBACKURL, SERVERPORT, and EXTRA. Enable the matching SaveClientID, SaveKey, SaveSecret, SaveCallBack, SaveServerPort, or SaveExtra flags when those values should be persisted with the tokens.

Reuse tokens after connecting

The component saves tokens automatically after a successful connection when PersistTokens is configured. On the next run, assign the same persistence settings before calling Connect; the component loads the stored values and validates or refreshes them as needed.

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCCloudDropBox1.Authentication.ClientID := 'your-dropbox-app-key';
  TMSFNCCloudDropBox1.Authentication.Secret := 'your-dropbox-app-secret';
  TMSFNCCloudDropBox1.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.
  TMSFNCCloudDropBox1.PersistTokens.Section := 'MyAppDropBox';

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

procedure TForm1.DropBoxConnected(Sender: TObject);
begin
  TMSFNCCloudDropBox1.GetAccountInfo;
end;

Store refresh tokens with the same care as passwords. Prefer encrypted application storage or OS credential storage when the application handles user accounts or shared machines.

See also