|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START admin_sdk_groups_migration_quickstart] |
| 16 | +import com.google.api.client.auth.oauth2.Credential; |
| 17 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 18 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 19 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 20 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 21 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 22 | +import com.google.api.client.http.ByteArrayContent; |
| 23 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 24 | +import com.google.api.client.json.JsonFactory; |
| 25 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 26 | +import com.google.api.client.util.store.FileDataStoreFactory; |
| 27 | +import com.google.api.services.groupsmigration.GroupsMigration; |
| 28 | +import com.google.api.services.groupsmigration.GroupsMigrationScopes; |
| 29 | +import com.google.api.services.groupsmigration.model.Groups; |
| 30 | + |
| 31 | +import javax.mail.Message; |
| 32 | +import javax.mail.MessagingException; |
| 33 | +import javax.mail.Session; |
| 34 | +import javax.mail.internet.InternetAddress; |
| 35 | +import javax.mail.internet.MimeMessage; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.InputStream; |
| 38 | +import java.io.InputStreamReader; |
| 39 | +import java.security.GeneralSecurityException; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +public class AdminSDKGroupsMigrationQuickstart { |
| 44 | + private static final String APPLICATION_NAME = "Google Admin SDK Groups Migration API Java Quickstart"; |
| 45 | + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
| 46 | + private static final String CREDENTIALS_FOLDER = "credentials"; // Directory to store user credentials. |
| 47 | + |
| 48 | + /** |
| 49 | + * Global instance of the scopes required by this quickstart. |
| 50 | + * If modifying these scopes, delete your previously saved credentials folder at /secret. |
| 51 | + */ |
| 52 | + private static final List<String> SCOPES = Collections.singletonList(GroupsMigrationScopes.APPS_GROUPS_MIGRATION); |
| 53 | + private static final String CLIENT_SECRET_DIR = "client_secret.json"; |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates an authorized Credential object. |
| 57 | + * @param HTTP_TRANSPORT The network HTTP Transport. |
| 58 | + * @return An authorized Credential object. |
| 59 | + * @throws IOException If there is no client_secret. |
| 60 | + */ |
| 61 | + private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException { |
| 62 | + // Load client secrets. |
| 63 | + InputStream in = AdminSDKGroupsMigrationQuickstart.class.getResourceAsStream(CLIENT_SECRET_DIR); |
| 64 | + GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
| 65 | + |
| 66 | + // Build flow and trigger user authorization request. |
| 67 | + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( |
| 68 | + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
| 69 | + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(CREDENTIALS_FOLDER))) |
| 70 | + .setAccessType("offline") |
| 71 | + .build(); |
| 72 | + return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); |
| 73 | + } |
| 74 | + |
| 75 | + public static void main(String... args) throws IOException, GeneralSecurityException, MessagingException { |
| 76 | + // Build a new authorized API client service. |
| 77 | + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
| 78 | + GroupsMigration service = new GroupsMigration.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) |
| 79 | + .setApplicationName(APPLICATION_NAME) |
| 80 | + .build(); |
| 81 | + |
| 82 | + System.out.println("Warning: A test email will be inserted " + |
| 83 | + "into the group entered below."); |
| 84 | + java.util.Scanner scanner = new java.util.Scanner(System.in); |
| 85 | + System.out.print("Enter the email address of a Google Group in your domain: "); |
| 86 | + String groupId = scanner.nextLine().trim(); |
| 87 | + |
| 88 | + // Insert a test email into the group. |
| 89 | + Session session = Session.getDefaultInstance(new java.util.Properties()); |
| 90 | + MimeMessage message = new MimeMessage(session); |
| 91 | + message.setSubject("Group Migration API Test"); |
| 92 | + message.setText("This is a test.\n"); |
| 93 | + message.setSentDate(new java.util.Date()); |
| 94 | + message.addRecipient(Message.RecipientType.TO, new InternetAddress(groupId)); |
| 95 | + message.setFrom(new InternetAddress("alice@example.com", "Alice Smith")); |
| 96 | + |
| 97 | + java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); |
| 98 | + message.writeTo(baos); |
| 99 | + ByteArrayContent content = new ByteArrayContent("message/rfc822", baos.toByteArray()); |
| 100 | + Groups result = service.archive().insert(groupId, content).execute(); |
| 101 | + System.out.printf("Result: %s\n", result.getResponseCode()); |
| 102 | + } |
| 103 | +} |
| 104 | +// [END admin_sdk_groups_migration_quickstart] |
0 commit comments