Table of Contents

Authentication and connecting

TTMSFNCCloudGoogleYouTube works with a YouTube channel through the YouTube Data API, so each session begins with OAuth 2.0 authentication. You register an OAuth client in the Google Cloud console (with the YouTube Data API enabled), give the component its client ID and secret, and it runs the consent flow. After the user approves access, the component holds the tokens that authorize listing, uploading, and commenting. This chapter covers the credentials, the requested scopes, token persistence, and confirming the connection.

OAuth credentials

Property Purpose
Authentication.ClientID The OAuth client ID from the Google Cloud console.
Authentication.Secret The matching client secret.
Authentication.CallBackURL The local redirect URI that receives the authorization code, e.g. http://127.0.0.1:8888/.

Set Logging := True while developing to trace the underlying requests.

Scopes and permissions

The Scopes property lists the OAuth scopes the component requests — these decide what the connected app may do (read the channel, upload videos, read and post comments). Review them so the consent screen asks for the access your app actually needs.

Token reuse across runs

Calling Connect the first time opens the system browser for the consent screen. To avoid repeating that, persist the tokens: set PersistTokens.Location to plIniFile and PersistTokens.Key to a writable file path, then call LoadTokens before connecting.

Connecting

Wire OnConnected before calling Connect. Connect is asynchronous — it returns immediately and raises OnConnected once authentication completes; issue your first request there.

procedure TForm1.SetupYouTube;
begin
  FYouTube := TTMSFNCCloudGoogleYouTube.Create(Self);

  // OAuth 2.0 client credentials from the Google Cloud console (YouTube Data API enabled).
  FYouTube.Authentication.ClientID := '<your-client-id>.apps.googleusercontent.com';
  FYouTube.Authentication.Secret := '<your-client-secret>';
  FYouTube.Authentication.CallBackURL := 'http://127.0.0.1:8888/';

  // Persist tokens so the consent screen only appears on first run.
  FYouTube.PersistTokens.Location := plIniFile;
  FYouTube.PersistTokens.Key := TTMSFNCUtils.AddBackslash(TTMSFNCUtils.GetDocumentsPath)
    + FYouTube.ClassName + '.ini';
  FYouTube.LoadTokens;

  FYouTube.OnConnected := YouTubeConnected;
  FYouTube.Connect; // opens the browser for consent on first run, then fires OnConnected
end;

procedure TForm1.YouTubeConnected(Sender: TObject);
begin
  // Tokens are valid here - safe to query or upload.
  FYouTube.GetAllVideos;
end;

Pitfalls

  • Query or upload only after OnConnected. Connect returns before authentication finishes.
  • Enable the YouTube Data API for the OAuth client, or every request is rejected.
  • The callback URL must match the registered redirect URI exactly, including the trailing slash if present.

See also