Bạn có thể sử dụng Merchant Accounts API để kiểm soát những người có quyền truy cập vào tài khoản người bán của bạn và cấp truy cập mà họ có.
User
là người có quyền truy cập vào tài khoản người bán của bạn. Bạn có thể sử dụng API Tài khoản người bán để xem, thêm và xoá người dùng khỏi tài khoản của mình.
Thêm người dùng vào tài khoản của bạn
Để thêm người dùng, hãy gọi phương thức accounts.users.create
và chỉ định cấp truy cập của họ trong trường access_rights
của User
.
Yêu cầu mẫu sau đây cho biết cách thêm người dùng mới có quyền STANDARD
và PERFORMANCE_REPORTING
:
POST https://merchantapi.googleapis.com/accounts/v1beta/accounts/{ACCOUNT_ID}/users?userId={USER_EMAILID}
{
"accessRights": [
"STANDARD",
"PERFORMANCE_REPORTING"
],
"name": "{NAME}"
}
Thay thế nội dung sau:
- {ACCOUNT_ID}: Mã nhận dạng duy nhất của tài khoản Merchant Center.
- {USER_EMAILID}: Địa chỉ email của người dùng mà bạn muốn thêm.
- {NAME}: Tên tài nguyên của người dùng ở định dạng
accounts/
{ACCOUNT_ID}/user/
{EMAIL_ADDRESS}.
Sau khi yêu cầu thành công, hệ thống sẽ trả về phản hồi sau:
{
"name": "accounts/{ACCOUNT_ID}/users/{USER_EMAILID}",
"state": "PENDING",
"accessRights": [
"STANDARD",
"PERFORMANCE_REPORTING"
]
}
Yêu cầu này sẽ gửi lời mời đến Tài khoản Google liên kết với người dùng mới. Người dùng đó phải chấp nhận lời mời thì mới được coi là thành viên của tài khoản.
Mẫu sau đây minh hoạ cách bạn có thể sử dụng phương thức CreateUserRequest
để thêm người dùng vào tài khoản của mình.
Java
public static void createUser(Config config, String email) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify where to insert the user.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
CreateUserRequest request =
CreateUserRequest.newBuilder()
.setParent(parent)
// This field is the email address of the user.
.setUserId(email)
.setUser(
User.newBuilder()
.addAccessRights(AccessRight.ADMIN)
.addAccessRights(AccessRight.PERFORMANCE_REPORTING)
.build())
.build();
System.out.println("Sending Create User request");
User response = userServiceClient.createUser(request);
System.out.println("Inserted User Name below");
// The last part of the user name will be the email address of the user.
// Format: `accounts/{account}/user/{user}`
System.out.println(response.getName());
} catch (Exception e) {
System.out.println(e);
}
}
Xem những người dùng có quyền truy cập vào tài khoản của bạn
Để xem tất cả người dùng có quyền truy cập vào tài khoản của bạn, hãy sử dụng phương thức accounts.users.list
.
Mẫu sau đây minh hoạ cách bạn có thể sử dụng phương thức ListUsersRequest
để liệt kê tất cả người dùng cho một tài khoản nhất định.
Java
public static void listUsers(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify the account from which to list all users.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
// The parent has the format: accounts/{account}
ListUsersRequest request = ListUsersRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list users request:");
ListUsersPagedResponse response = userServiceClient.listUsers(request);
int count = 0;
// Iterates over all rows in all pages and prints the user
// in each row.
// `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
// request to fetch all pages of data.
for (User element : response.iterateAll()) {
System.out.println(element);
count++;
}
System.out.print("The following count of elements were returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
}
Truy xuất một người dùng cho một tài khoản nhất định
Mẫu sau đây minh hoạ cách bạn có thể sử dụng phương thức GetUserRequest
để truy xuất người dùng cho một tài khoản nhất định.
Java
public static void getUser(Config config, String email) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates user name to identify user.
String name =
UserName.newBuilder()
.setAccount(config.getAccountId().toString())
.setEmail(email)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
// The name has the format: accounts/{account}/users/{email}
GetUserRequest request = GetUserRequest.newBuilder().setName(name).build();
System.out.println("Sending Get user request:");
User response = userServiceClient.getUser(request);
System.out.println("Retrieved User below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
Xoá người dùng khỏi một tài khoản nhất định
Để xoá một người dùng khỏi tài khoản của bạn, hãy tạo yêu cầu bằng phương thức accounts.users.delete
.
Mẫu sau đây minh hoạ cách bạn có thể sử dụng phương thức DeleteUserRequest
để xoá người dùng khỏi một tài khoản nhất định.
Java
public static void deleteUser(Config config, String email) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates user name to identify the user.
String name =
UserName.newBuilder()
.setAccount(config.getAccountId().toString())
.setEmail(email)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
DeleteUserRequest request = DeleteUserRequest.newBuilder().setName(name).build();
System.out.println("Sending Delete User request");
userServiceClient.deleteUser(request); // no response returned on success
System.out.println("Delete successful.");
} catch (Exception e) {
System.out.println(e);
}
}
Thay đổi cấp truy cập của người dùng
Để thay đổi cấp truy cập của người dùng, hãy gọi phương thức accounts.users.patch
với cấp truy cập mới.
Mẫu sau đây minh hoạ cách bạn có thể sử dụng phương thức UpdateUserSample
để cập nhật người dùng thành quản trị viên của một tài khoản nhất định.
Java
public static void updateUser(Config config, String email, AccessRight accessRight)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates user name to identify user.
String name =
UserName.newBuilder()
.setAccount(config.getAccountId().toString())
.setEmail(email)
.build()
.toString();
// Create a user with the updated fields.
User user = User.newBuilder().setName(name).addAccessRights(accessRight).build();
FieldMask fieldMask = FieldMask.newBuilder().addPaths("access_rights").build();
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
UpdateUserRequest request =
UpdateUserRequest.newBuilder().setUser(user).setUpdateMask(fieldMask).build();
System.out.println("Sending Update User request");
User response = userServiceClient.updateUser(request);
System.out.println("Updated User Name below");
System.out.println(response.getName());
} catch (Exception e) {
System.out.println(e);
}
}
So sánh giữa quyền quản trị cấp cao, quyền quản trị và quyền truy cập tiêu chuẩn
Bạn không thể xoá người dùng có quyền quản trị cấp cao đối với Business Manager khỏi Merchant Center. Để biết thêm thông tin về quyền quản trị viên cấp cao, hãy xem bài viết Quản lý doanh nghiệp trong vai trò quản trị viên cấp cao.
Một số phương thức (chẳng hạn như các phương thức ghi dữ liệu tài khoản) yêu cầu quyền truy cập của quản trị viên. Để tìm hiểu cấp truy cập bắt buộc cho từng phương thức, hãy xem tài liệu tham khảo.
Nếu không chỉ định cấp truy cập, bạn có thể sử dụng phương thức có quyền truy cập tiêu chuẩn.
Bước tiếp theo
- Tìm hiểu cách tạo và quản lý tài khoản phụ.
- Để hiểu cách hoạt động của mối quan hệ giữa các tài khoản người bán, hãy xem bài viết Mối quan hệ giữa các tài khoản.