1- // Copyright 2021 Google LLC
1+ // Copyright 2022 Google LLC
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -34,13 +34,19 @@ public class EnableAutoReply {
3434 /**
3535 * Enables the auto reply
3636 *
37- * @throws IOException
37+ * @return the reply message and response metadata.
38+ * @throws IOException - if service account credentials file not found.
3839 */
39- public static void autoReply () throws IOException {
40- // Load pre-authorized user credentials from the environment.
41- // TODO(developer) - See https://developers.google.com/identity for
42- // guides on implementing OAuth2 for your application.
43- GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ().createScoped (Collections .singleton (GmailScopes .GMAIL_SETTINGS_BASIC ));
40+ public static VacationSettings autoReply () throws IOException {
41+ // TODO(developer) - Replace with your email address.
42+ String USER_EMAIL_ADDRESS = "gduser1@workspacesamples.dev" ;
43+
44+ /* Load pre-authorized user credentials from the environment.
45+ TODO(developer) - See https://developers.google.com/identity for
46+ guides on implementing OAuth2 for your application. */
47+ GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ()
48+ .createScoped (Collections .singleton (GmailScopes .GMAIL_SETTINGS_BASIC ))
49+ .createDelegated (USER_EMAIL_ADDRESS );
4450 HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter (
4551 credentials );
4652
@@ -57,12 +63,16 @@ public static void autoReply() throws IOException{
5763 .setEnableAutoReply (true )
5864 .setResponseBodyHtml ("I am on vacation and will reply when I am back in the office. Thanks!" )
5965 .setRestrictToDomain (true )
60- .setStartTime (LocalDateTime .now ().toEpochSecond (ZoneOffset .from (ZonedDateTime .now ())) * 1000 )
61- .setEndTime (LocalDateTime .now ().plusDays (7 ).toEpochSecond (ZoneOffset .from (ZonedDateTime .now ())) * 1000 );
66+ .setStartTime (LocalDateTime .now ()
67+ .toEpochSecond (ZoneOffset .from (ZonedDateTime .now ())) * 1000 )
68+ .setEndTime (LocalDateTime .now ().plusDays (7 )
69+ .toEpochSecond (ZoneOffset .from (ZonedDateTime .now ())) * 1000 );
6270
63- VacationSettings response = service .users ().settings ().updateVacation ("me" , vacationSettings ).execute ();
71+ VacationSettings response = service .users ().settings ()
72+ .updateVacation ("me" , vacationSettings ).execute ();
6473 // Prints the auto-reply response body
6574 System .out .println ("Enabled auto reply with message : " +response .getResponseBodyHtml ());
75+ return response ;
6676 } catch (GoogleJsonResponseException e ) {
6777 // TODO(developer) - handle error appropriately
6878 System .err .println ("Unable to enable auto reply: " + e .getDetails ());
0 commit comments