Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Authorize a desktop application to use Gmail

See more GMail REST API Examples

Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.

Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.

Google documentation

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
oauth: HCkOAuth2;
authUrl: PWideChar;
waited: Integer;
token: HCkJsonObject;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

oauth := CkOAuth2_Create();
CkOAuth2_putTokenEndpoint(oauth,'https://oauth2.googleapis.com/token');
CkOAuth2_putClientId(oauth,'YOUR_DESKTOP_CLIENT_ID');
CkOAuth2_putClientSecret(oauth,'YOUR_DESKTOP_CLIENT_SECRET');

CkOAuth2_putAuthorizationEndpoint(oauth,'https://accounts.google.com/o/oauth2/v2/auth');
CkOAuth2_putLocalHost(oauth,'127.0.0.1');
CkOAuth2_putListenPort(oauth,55568);
CkOAuth2_putListenPortRangeEnd(oauth,55578);
CkOAuth2_putCodeChallenge(oauth,True);
CkOAuth2_putCodeChallengeMethod(oauth,'S256');
CkOAuth2_putScope(oauth,'https://www.googleapis.com/auth/gmail.readonly');
success := CkOAuth2_AddAuthQueryParam(oauth,'access_type','offline');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not configure offline access.');
    Exit;
  end;
//  This is the interactive setup step. Subsequent refreshes use the saved refresh token.
success := CkOAuth2_AddAuthQueryParam(oauth,'prompt','consent');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not configure consent.');
    Exit;
  end;
authUrl := CkOAuth2__startAuth(oauth);
if (CkOAuth2_getLastMethodSuccess(oauth) <> True) then
  begin
    Memo1.Lines.Add('Could not start the loopback listener.');
    Exit;
  end;
success := CkOAuth2_LaunchBrowser(oauth,authUrl);
if (success <> True) then
  begin
    CkOAuth2_Cancel(oauth);
    Memo1.Lines.Add('Could not open the system browser.');
    Exit;
  end;
waited := 0;
while ((CkOAuth2_getAuthFlowState(oauth) < 3) and (waited < 180000)) do
  begin
    CkOAuth2_SleepMs(oauth,200);
    waited := waited + 200;
  end;

if (CkOAuth2_getAuthFlowState(oauth) < 3) then
  begin
    CkOAuth2_Cancel(oauth);
    Memo1.Lines.Add('Authorization timed out.');
    Exit;
  end;
if (CkOAuth2_getAuthFlowState(oauth) <> 3) then
  begin
    Memo1.Lines.Add('Authorization did not complete. Flow state: ' + IntToStr(CkOAuth2_getAuthFlowState(oauth)));
    Exit;
  end;
token := CkJsonObject_Create();
success := CkJsonObject_Load(token,CkOAuth2__accessTokenResponse(oauth));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not parse the token response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(token,'refresh_token') <> 1) or (CkJsonObject_StringOfEquals(token,'refresh_token','',True) = True)) then
  begin
    Memo1.Lines.Add('No offline refresh token was issued. The existing cache was preserved.');
    Exit;
  end;
success := CkJsonObject_WriteFile(token,'gmail-token.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not save the token file.');
    Exit;
  end;
Memo1.Lines.Add('Credentials saved in the private working directory.');

CkOAuth2_Dispose(oauth);
CkJsonObject_Dispose(token);