Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions gmail/snippets/src/main/java/UpdateSignature.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);

Expand All @@ -59,14 +65,15 @@ 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(),
aliasSettings)
.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());
Expand Down
27 changes: 27 additions & 0 deletions gmail/snippets/src/test/java/TestUpdateSignature.java
Original file line number Diff line number Diff line change
@@ -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);
}
}