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.
@@ -32,13 +32,19 @@ public class UpdateSignature {
3232 /**
3333 * Update the gmail signature.
3434 *
35- * @throws IOException
35+ * @return the updated signature id
36+ * @throws IOException - if service account credentials file not found.
3637 */
37- public static void updateGmailSignature () throws IOException {
38- // Load pre-authorized user credentials from the environment.
39- // TODO(developer) - See https://developers.google.com/identity for
40- // guides on implementing OAuth2 for your application.
41- GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ().createScoped (Collections .singleton (GmailScopes .GMAIL_SETTINGS_BASIC ));
38+ public static String updateGmailSignature () throws IOException {
39+ // TODO(developer) - Replace with your email address.
40+ String USER_EMAIL_ADDRESS = "gduser1@workspacesamples.dev" ;
41+
42+ /* Load pre-authorized user credentials from the environment.
43+ TODO(developer) - See https://developers.google.com/identity for
44+ guides on implementing OAuth2 for your application. */
45+ GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ()
46+ .createScoped (Collections .singleton (GmailScopes .GMAIL_SETTINGS_BASIC ))
47+ .createDelegated (USER_EMAIL_ADDRESS );
4248 HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter (
4349 credentials );
4450
@@ -59,14 +65,15 @@ public static void updateGmailSignature() throws IOException {
5965 }
6066 }
6167 // Updating a new signature
62- SendAs aliasSettings = new SendAs ().setSignature ("I heart cats. " );
68+ SendAs aliasSettings = new SendAs ().setSignature ("Automated Signature " );
6369 SendAs result = service .users ().settings ().sendAs ().patch (
6470 "me" ,
6571 primaryAlias .getSendAsEmail (),
6672 aliasSettings )
6773 .execute ();
6874 //Prints the updated signature
6975 System .out .println ("Updated signature - " + result .getSignature ());
76+ return result .getSignature ();
7077 } catch (GoogleJsonResponseException e ) {
7178 // TODO(developer) - handle error appropriately
7279 System .err .println ("Unable to update signature: " + e .getDetails ());
0 commit comments