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.
Chilkat PHP Downloads
<?php
use Chilkat2\JsonObject;
use Chilkat2\OAuth2;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$oauth = new OAuth2();
$oauth->TokenEndpoint = 'https://oauth2.googleapis.com/token';
$oauth->ClientId = 'YOUR_DESKTOP_CLIENT_ID';
$oauth->ClientSecret = 'YOUR_DESKTOP_CLIENT_SECRET';
$oauth->AuthorizationEndpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
$oauth->LocalHost = '127.0.0.1';
$oauth->ListenPort = 55568;
$oauth->ListenPortRangeEnd = 55578;
$oauth->CodeChallenge = true;
$oauth->CodeChallengeMethod = 'S256';
$oauth->Scope = 'https://www.googleapis.com/auth/gmail.readonly';
try {
$oauth->AddAuthQueryParam('access_type', 'offline');
} catch (\Chilkat2\Exception $e) {
echo "Could not configure offline access.\n";
exit;
}
// This is the interactive setup step. Subsequent refreshes use the saved refresh token.
try {
$oauth->AddAuthQueryParam('prompt', 'consent');
} catch (\Chilkat2\Exception $e) {
echo "Could not configure consent.\n";
exit;
}
try {
$authUrl = $oauth->StartAuth();
} catch (\Chilkat2\Exception $e) {
echo "Could not start the loopback listener.\n";
exit;
}
try {
$oauth->LaunchBrowser($authUrl);
} catch (\Chilkat2\Exception $e) {
$oauth->Cancel();
echo "Could not open the system browser.\n";
exit;
}
$waited = 0;
while (($oauth->AuthFlowState < 3) && ($waited < 180000)) {
$oauth->SleepMs(200);
$waited = $waited + 200;
}
if ($oauth->AuthFlowState < 3) {
$oauth->Cancel();
echo "Authorization timed out.\n";
exit;
}
if ($oauth->AuthFlowState != 3) {
echo 'Authorization did not complete. Flow state: ', $oauth->AuthFlowState, "\n";
exit;
}
$token = new JsonObject();
try {
$token->Load($oauth->AccessTokenResponse);
} catch (\Chilkat2\Exception $e) {
echo "Could not parse the token response.\n";
exit;
}
if (($token->JsonTypeOf('refresh_token') != 1) || ($token->StringOfEquals('refresh_token', '', true))) {
echo "No offline refresh token was issued. The existing cache was preserved.\n";
exit;
}
try {
$token->WriteFile('gmail-token.json');
} catch (\Chilkat2\Exception $e) {
echo "Could not save the token file.\n";
exit;
}
echo "Credentials saved in the private working directory.\n";