Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
15 changes: 10 additions & 5 deletions gmail/snippets/src/main/java/EnableForwarding.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ public class EnableForwarding {
* @throws IOException if service account credentials file not found.
*/
public static AutoForwarding enableAutoForwarding(String forwardingEmail) 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.singletonList(GmailScopes.GMAIL_SETTINGS_SHARING));
// 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.singletonList(GmailScopes.GMAIL_SETTINGS_SHARING))
.createDelegated(USER_EMAIL_ADDRESS);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);

Expand Down Expand Up @@ -74,4 +79,4 @@ public static AutoForwarding enableAutoForwarding(String forwardingEmail) throws
return null;
}
}
// [END gmail_enable_forwarding]
// [END gmail_enable_forwarding]
31 changes: 31 additions & 0 deletions gmail/snippets/src/test/java/TestEnableForwarding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.AutoForwarding;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertNotNull;

public class TestEnableForwarding extends BaseTest {

@Test
public void TestEnableAutoForwarding() throws IOException {
AutoForwarding forwarding = EnableForwarding.enableAutoForwarding(FORWARDING_ADDRESS);
assertNotNull(forwarding);
forwarding = new AutoForwarding().setEnabled(false);
this.service.users().settings().updateAutoForwarding("me", forwarding).execute();
this.service.users().settings().forwardingAddresses().delete("me", FORWARDING_ADDRESS).execute();
}
}