Skip to content

Commit cfb3a31

Browse files
PierrickVouletpierrick
and
pierrick
authored
feat: add create and set up space code samples (#1165)
* feat: add create and set up space code samples * Change for immutable list --------- Co-authored-by: pierrick <pierrick@google.com>
1 parent b2f6fae commit cfb3a31

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.common.collect.ImmutableList;
20+
import com.google.protobuf.util.JsonFormat;
21+
// [START chat_create_space_user_cred]
22+
import com.google.chat.v1.ChatServiceClient;
23+
import com.google.chat.v1.CreateSpaceRequest;
24+
import com.google.chat.v1.Space;
25+
26+
// This sample shows how to create space with user credential.
27+
public class CreateSpaceUserCred {
28+
29+
private static final String SCOPE =
30+
"https://www.googleapis.com/auth/chat.spaces.create";
31+
32+
public static void main(String[] args) throws Exception {
33+
try (ChatServiceClient chatServiceClient =
34+
AuthenticationUtils.createClientWithUserCredentials(
35+
ImmutableList.of(SCOPE))) {
36+
CreateSpaceRequest.Builder request = CreateSpaceRequest.newBuilder()
37+
.setSpace(Space.newBuilder()
38+
.setSpaceType(Space.SpaceType.SPACE)
39+
// Replace DISPLAY_NAME here.
40+
.setDisplayName("DISPLAY_NAME"));
41+
Space response = chatServiceClient.createSpace(request.build());
42+
43+
System.out.println(JsonFormat.printer().print(response));
44+
}
45+
}
46+
}
47+
// [END chat_create_space_user_cred]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.common.collect.ImmutableList;
20+
import com.google.protobuf.util.JsonFormat;
21+
22+
// [START chat_set_up_space_user_cred]
23+
import com.google.chat.v1.ChatServiceClient;
24+
import com.google.chat.v1.Membership;
25+
import com.google.chat.v1.SetUpSpaceRequest;
26+
import com.google.chat.v1.Space;
27+
import com.google.chat.v1.User;
28+
29+
// This sample shows how to set up a named space with one initial member with
30+
// user credential.
31+
public class SetUpSpaceUserCred {
32+
33+
private static final String SCOPE =
34+
"https://www.googleapis.com/auth/chat.spaces.create";
35+
36+
public static void main(String[] args) throws Exception {
37+
try (ChatServiceClient chatServiceClient =
38+
AuthenticationUtils.createClientWithUserCredentials(
39+
ImmutableList.of(SCOPE))) {
40+
SetUpSpaceRequest.Builder request = SetUpSpaceRequest.newBuilder()
41+
.setSpace(Space.newBuilder()
42+
.setSpaceType(Space.SpaceType.SPACE)
43+
// Replace DISPLAY_NAME here.
44+
.setDisplayName("DISPLAY_NAME"))
45+
.addAllMemberships(ImmutableList.of(Membership.newBuilder()
46+
.setMember(User.newBuilder()
47+
// Replace USER_NAME here.
48+
.setName("users/USER_NAME")
49+
.setType(User.Type.HUMAN)).build()));
50+
Space response = chatServiceClient.setUpSpace(request.build());
51+
52+
System.out.println(JsonFormat.printer().print(response));
53+
}
54+
}
55+
}
56+
// [END chat_set_up_space_user_cred]

0 commit comments

Comments
 (0)