|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.workspace.api.chat.samples; |
| 18 | + |
| 19 | +import com.google.auth.oauth2.AccessToken; |
| 20 | +import com.google.auth.oauth2.GoogleCredentials; |
| 21 | +import com.google.auth.oauth2.ServiceAccountCredentials; |
| 22 | +import com.google.auth.oauth2.UserCredentials; |
| 23 | +import com.google.api.client.auth.oauth2.Credential; |
| 24 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 25 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 26 | +import com.google.api.client.json.gson.GsonFactory; |
| 27 | +import com.google.api.client.json.JsonFactory; |
| 28 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 29 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 30 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 31 | +import com.google.api.gax.core.GoogleCredentialsProvider; |
| 32 | +import com.google.api.gax.core.FixedCredentialsProvider; |
| 33 | +import com.google.chat.v1.ChatServiceClient; |
| 34 | +import com.google.chat.v1.ChatServiceSettings; |
| 35 | +import com.google.common.collect.ImmutableList; |
| 36 | + |
| 37 | +import java.io.FileInputStream; |
| 38 | +import java.io.FileReader; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.Date; |
| 41 | + |
| 42 | +public class AuthenticationUtils{ |
| 43 | + |
| 44 | + private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); |
| 45 | + private static final String SERVICE_ACCOUNT_FILE = "service_account.json"; |
| 46 | + private static final String CLIENT_SECRET_FILE = "client_secrets.json"; |
| 47 | + private static final String APP_AUTH_OAUTH_SCOPE = |
| 48 | + "https://www.googleapis.com/auth/chat.bot"; |
| 49 | + |
| 50 | + public static ChatServiceClient createClientWithAppCredentials() |
| 51 | + throws Exception { |
| 52 | + // For more information on service account authentication, see |
| 53 | + // https://developers.google.com/workspace/chat/authenticate-authorize-chat-app |
| 54 | + GoogleCredentials credential = ServiceAccountCredentials.fromStream( |
| 55 | + new FileInputStream(SERVICE_ACCOUNT_FILE)) |
| 56 | + .createScoped(ImmutableList.of(APP_AUTH_OAUTH_SCOPE)); |
| 57 | + |
| 58 | + // Create the ChatServiceSettings with the app credentials |
| 59 | + ChatServiceSettings chatServiceSettings = |
| 60 | + ChatServiceSettings.newBuilder() |
| 61 | + .setCredentialsProvider( |
| 62 | + FixedCredentialsProvider.create(credential)) |
| 63 | + .build(); |
| 64 | + |
| 65 | + return ChatServiceClient.create(chatServiceSettings); |
| 66 | + } |
| 67 | + |
| 68 | + public static ChatServiceClient createClientWithUserCredentials( |
| 69 | + ImmutableList<String> scopes) throws Exception { |
| 70 | + // For more information on user authentication, see |
| 71 | + // https://developers.google.com/workspace/chat/authenticate-authorize-chat-user |
| 72 | + GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( |
| 73 | + JSON_FACTORY, new FileReader(CLIENT_SECRET_FILE)); |
| 74 | + |
| 75 | + Credential credential = authorize(scopes, clientSecrets); |
| 76 | + |
| 77 | + AccessToken accessToken = new AccessToken( |
| 78 | + credential.getAccessToken(), |
| 79 | + new Date( |
| 80 | + // put the actual expiry date of access token here |
| 81 | + System.currentTimeMillis())); |
| 82 | + UserCredentials googleCredentials = |
| 83 | + UserCredentials |
| 84 | + .newBuilder() |
| 85 | + .setAccessToken(accessToken) |
| 86 | + .setRefreshToken(credential.getRefreshToken()) |
| 87 | + .setClientId(clientSecrets.getInstalled().getClientId()) |
| 88 | + .setClientSecret(clientSecrets.getInstalled().getClientSecret()) |
| 89 | + .build(); |
| 90 | + |
| 91 | + // Create the ChatServiceSettings with the credentials |
| 92 | + ChatServiceSettings chatServiceSettings = |
| 93 | + ChatServiceSettings |
| 94 | + .newBuilder() |
| 95 | + .setCredentialsProvider( |
| 96 | + FixedCredentialsProvider.create(googleCredentials)) |
| 97 | + .build(); |
| 98 | + |
| 99 | + return ChatServiceClient.create(chatServiceSettings); |
| 100 | + } |
| 101 | + |
| 102 | + // Generate access token and refresh token using scopes and client secrets. |
| 103 | + private static Credential authorize( |
| 104 | + ImmutableList<String> scopes, GoogleClientSecrets clientSecrets) |
| 105 | + throws Exception { |
| 106 | + // Set up authorization code flow. |
| 107 | + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( |
| 108 | + GoogleNetHttpTransport.newTrustedTransport(), |
| 109 | + JSON_FACTORY, |
| 110 | + clientSecrets, |
| 111 | + scopes) |
| 112 | + // Set these two options to generate refresh token alongside access token. |
| 113 | + .setAccessType("offline") |
| 114 | + .setApprovalPrompt("force") |
| 115 | + .build(); |
| 116 | + |
| 117 | + // Authorize. |
| 118 | + return new AuthorizationCodeInstalledApp( |
| 119 | + flow, new LocalServerReceiver()).authorize("user"); |
| 120 | + } |
| 121 | +} |
0 commit comments