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 Objective-C Downloads
#import <CkoOAuth2.h>
#import <NSString.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoOAuth2 *oauth = [[CkoOAuth2 alloc] init];
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 = [NSNumber numberWithInt:55568];
oauth.ListenPortRangeEnd = [NSNumber numberWithInt:55578];
oauth.CodeChallenge = YES;
oauth.CodeChallengeMethod = @"S256";
oauth.Scope = @"https://www.googleapis.com/auth/gmail.readonly";
success = [oauth AddAuthQueryParam: @"access_type" value: @"offline"];
if (success != YES) {
NSLog(@"%@",@"Could not configure offline access.");
return;
}
// This is the interactive setup step. Subsequent refreshes use the saved refresh token.
success = [oauth AddAuthQueryParam: @"prompt" value: @"consent"];
if (success != YES) {
NSLog(@"%@",@"Could not configure consent.");
return;
}
NSString *authUrl = [oauth StartAuth];
if (oauth.LastMethodSuccess != YES) {
NSLog(@"%@",@"Could not start the loopback listener.");
return;
}
success = [oauth LaunchBrowser: authUrl];
if (success != YES) {
[oauth Cancel];
NSLog(@"%@",@"Could not open the system browser.");
return;
}
int waited = 0;
while ((([oauth.AuthFlowState intValue] < 3) && (waited < 180000))) {
[oauth SleepMs: [NSNumber numberWithInt: 200]];
waited = waited + 200;
}
if ([oauth.AuthFlowState intValue] < 3) {
[oauth Cancel];
NSLog(@"%@",@"Authorization timed out.");
return;
}
if ([oauth.AuthFlowState intValue] != 3) {
NSLog(@"%@%d",@"Authorization did not complete. Flow state: ",[oauth.AuthFlowState intValue]);
return;
}
CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token Load: oauth.AccessTokenResponse];
if (success != YES) {
NSLog(@"%@",@"Could not parse the token response.");
return;
}
if (([[token JsonTypeOf: @"refresh_token"] intValue] != 1) || ([token StringOfEquals: @"refresh_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"No offline refresh token was issued. The existing cache was preserved.");
return;
}
success = [token WriteFile: @"gmail-token.json"];
if (success != YES) {
NSLog(@"%@",@"Could not save the token file.");
return;
}
NSLog(@"%@",@"Credentials saved in the private working directory.");