diff --git a/gmail/snippets/src/main/java/EnableAutoReply.java b/gmail/snippets/src/main/java/EnableAutoReply.java index ef6e052c..33ffd77b 100644 --- a/gmail/snippets/src/main/java/EnableAutoReply.java +++ b/gmail/snippets/src/main/java/EnableAutoReply.java @@ -1,4 +1,4 @@ -// Copyright 2021 Google LLC +// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -34,13 +34,19 @@ public class EnableAutoReply { /** * Enables the auto reply * - * @throws IOException + * @return the reply message and response metadata. + * @throws IOException - if service account credentials file not found. */ - public static void autoReply() throws IOException{ - // Load pre-authorized user credentials from the environment. - // TODO(developer) - See https://developers.google.com/identity for - // guides on implementing OAuth2 for your application. - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC)); + public static VacationSettings autoReply() throws IOException{ + // TODO(developer) - Replace with your email address. + String USER_EMAIL_ADDRESS = "gduser1@workspacesamples.dev"; + + /* Load pre-authorized user credentials from the environment. + TODO(developer) - See https://developers.google.com/identity for + guides on implementing OAuth2 for your application. */ + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC)) + .createDelegated(USER_EMAIL_ADDRESS); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); @@ -57,12 +63,16 @@ public static void autoReply() throws IOException{ .setEnableAutoReply(true) .setResponseBodyHtml("I am on vacation and will reply when I am back in the office. Thanks!") .setRestrictToDomain(true) - .setStartTime(LocalDateTime.now().toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000) - .setEndTime(LocalDateTime.now().plusDays(7).toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000); + .setStartTime(LocalDateTime.now() + .toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000) + .setEndTime(LocalDateTime.now().plusDays(7) + .toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000); - VacationSettings response = service.users().settings().updateVacation("me", vacationSettings).execute(); + VacationSettings response = service.users().settings() + .updateVacation("me", vacationSettings).execute(); // Prints the auto-reply response body System.out.println("Enabled auto reply with message : "+response.getResponseBodyHtml()); + return response; } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately System.err.println("Unable to enable auto reply: " + e.getDetails()); diff --git a/gmail/snippets/src/test/java/TestEnableAutoReply.java b/gmail/snippets/src/test/java/TestEnableAutoReply.java new file mode 100644 index 00000000..04778cfa --- /dev/null +++ b/gmail/snippets/src/test/java/TestEnableAutoReply.java @@ -0,0 +1,28 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +import com.google.api.services.gmail.model.VacationSettings; +import org.junit.Test; +import java.io.IOException; +import static org.junit.Assert.assertNotNull; + +public class TestEnableAutoReply extends EnableAutoReply { + + @Test + public void testAutoReply() throws IOException { + VacationSettings settings = EnableAutoReply.autoReply(); + assertNotNull(settings); + } +} \ No newline at end of file