@@ -0,0 +1,3128 @@
+// Copyright 2021 Google LLC%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     https://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0A%0A// %5BSTART gmail_create_filter%5D%0Aimport com.google.api.client.googleapis.json.GoogleJsonResponseException;%0Aimport com.google.api.client.http.HttpRequestInitializer;%0Aimport com.google.api.client.http.javanet.NetHttpTransport;%0Aimport com.google.api.client.json.gson.GsonFactory;%0Aimport com.google.api.services.gmail.Gmail;%0Aimport com.google.api.services.gmail.GmailScopes;%0Aimport com.google.api.services.gmail.model.Filter;%0Aimport com.google.api.services.gmail.model.FilterAction;%0Aimport com.google.api.services.gmail.model.FilterCriteria;%0Aimport com.google.auth.http.HttpCredentialsAdapter;%0Aimport com.google.auth.oauth2.GoogleCredentials;%0Aimport java.io.IOException;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0A%0A/* Class to demonstrate the use of Gmail Create Filter API */%0Apublic class CreateFilter %7B%0A    /**%0A     * Create a new filter.%0A     *%0A     * @throws IOException%0A     */%0A    public static void createNewFilter() throws IOException %7B%0A        // Load pre-authorized user credentials from the environment.%0A        // TODO(developer) - See https://developers.google.com/identity for%0A        // guides on implementing OAuth2 for your application.%0A        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC));%0A        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(%0A                credentials);%0A%0A        // Create the gmail API client%0A        Gmail service = new Gmail.Builder(new NetHttpTransport(),%0A                GsonFactory.getDefaultInstance(),%0A                requestInitializer)%0A                .setApplicationName(%22Gmail samples%22)%0A                .build();%0A%0A        // Filter the mail from sender and archive them(skip the inbox)%0A        try %7B%0A            Filter filter = new Filter()%0A                    .setCriteria(new FilterCriteria()%0A                            .setFrom(%22gduser1@workspacesamples.dev%22))%0A                    .setAction(new FilterAction()%0A                            .setRemoveLabelIds(Arrays.asList(%22INBOX%22)));%0A%0A            Filter result = service.users().settings().filters().create(%22me%22, filter).execute();%0A            // Prints the new created filter ID%0A            System.out.println(%22Created filter %22 + result.getId());%0A        %7D catch (GoogleJsonResponseException e) %7B%0A            // TODO(developer) - handle error appropriately%0A            System.err.println(%22Unable to create filter: %22 + e.getDetails());%0A            throw e;%0A        %7D%0A    %7D%0A%7D%0A// %5BEND gmail_create_filter%5D

Create Filter gmail snippet (#161)