Skip to content

Commit b2f6fae

Browse files
PierrickVouletpierrick
and
pierrick
authored
feat: add create membership for group code sample (#1164)
Co-authored-by: pierrick <[email protected]>
1 parent 67f9f30 commit b2f6fae

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_membership_user_cred_for_group]
22+
import com.google.chat.v1.ChatServiceClient;
23+
import com.google.chat.v1.CreateMembershipRequest;
24+
import com.google.chat.v1.Membership;
25+
import com.google.chat.v1.SpaceName;
26+
import com.google.chat.v1.Group;
27+
28+
// This sample shows how to create membership with user credential for a group.
29+
public class CreateMembershipUserCredForGroup {
30+
31+
private static final String SCOPE =
32+
"https://www.googleapis.com/auth/chat.memberships";
33+
34+
public static void main(String[] args) throws Exception {
35+
try (ChatServiceClient chatServiceClient =
36+
AuthenticationUtils.createClientWithUserCredentials(
37+
ImmutableList.of(SCOPE))) {
38+
CreateMembershipRequest.Builder request = CreateMembershipRequest.newBuilder()
39+
// replace SPACE_NAME here
40+
.setParent("spaces/SPACE_NAME")
41+
.setMembership(Membership.newBuilder()
42+
.setGroupMember(Group.newBuilder()
43+
// replace GROUP_NAME here
44+
.setName("groups/GROUP_NAME")));
45+
Membership response = chatServiceClient.createMembership(request.build());
46+
47+
System.out.println(JsonFormat.printer().print(response));
48+
}
49+
}
50+
}
51+
// [END chat_create_membership_user_cred_for_group]

0 commit comments

Comments
 (0)