diff --git a/gmail/snippets/src/main/java/UpdateSignature.java b/gmail/snippets/src/main/java/UpdateSignature.java index b6ca985e..9bc29454 100644 --- a/gmail/snippets/src/main/java/UpdateSignature.java +++ b/gmail/snippets/src/main/java/UpdateSignature.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. @@ -32,13 +32,19 @@ public class UpdateSignature { /** * Update the gmail signature. * - * @throws IOException + * @return the updated signature id + * @throws IOException - if service account credentials file not found. */ - public static void updateGmailSignature() 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 String updateGmailSignature() 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); @@ -59,7 +65,7 @@ public static void updateGmailSignature() throws IOException { } } // Updating a new signature - SendAs aliasSettings = new SendAs().setSignature("I heart cats."); + SendAs aliasSettings = new SendAs().setSignature("Automated Signature"); SendAs result = service.users().settings().sendAs().patch( "me", primaryAlias.getSendAsEmail(), @@ -67,6 +73,7 @@ public static void updateGmailSignature() throws IOException { .execute(); //Prints the updated signature System.out.println("Updated signature - " + result.getSignature()); + return result.getSignature(); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately System.err.println("Unable to update signature: " + e.getDetails()); diff --git a/gmail/snippets/src/test/java/TestUpdateSignature.java b/gmail/snippets/src/test/java/TestUpdateSignature.java new file mode 100644 index 00000000..ea5bff3e --- /dev/null +++ b/gmail/snippets/src/test/java/TestUpdateSignature.java @@ -0,0 +1,27 @@ +// 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 org.junit.Test; +import java.io.IOException; +import static org.junit.Assert.assertEquals; + +public class TestUpdateSignature extends BaseTest{ + + @Test + public void testUpdateGmailSignature() throws IOException{ + String signature = UpdateSignature.updateGmailSignature(); + assertEquals("Automated Signature", signature); + } +} \ No newline at end of file