控制对您账号的访问权限

您可以使用 Merchant Accounts API 来控制哪些人有权访问您的商家账号,以及他们拥有哪种访问权限级别

User 是指有权访问您的商家账号的人员。您可以使用 Merchant Accounts API 查看、添加和移除账号中的用户。

向账号中添加用户

如需添加用户,请调用 accounts.users.create 方法,并在 Useraccess_rights 字段中指定其访问权限级别

以下示例请求展示了如何添加具有 STANDARDPERFORMANCE_REPORTING 权限的新用户:

POST https://merchantapi.googleapis.com/accounts/v1beta/accounts/{ACCOUNT_ID}/users?userId={USER_EMAILID}

{
  "accessRights": [
    "STANDARD",
    "PERFORMANCE_REPORTING"
  ],
  "name": "{NAME}"
}

替换以下内容:

  • {ACCOUNT_ID}:您的 Merchant Center 账号的唯一标识符。
  • {USER_EMAILID}:您要添加的用户的电子邮件地址。
  • {NAME}:用户的资源名称,格式为 accounts/{ACCOUNT_ID}/user/{EMAIL_ADDRESS}

请求成功后,系统会返回以下响应:

{
  "name": "accounts/{ACCOUNT_ID}/users/{USER_EMAILID}",
  "state": "PENDING",
  "accessRights": [
    "STANDARD",
    "PERFORMANCE_REPORTING"
  ]
}

此请求会向与新用户关联的 Google 账号发送邀请,新用户必须接受邀请,您才会将其视为您账号的成员。

以下示例演示了如何使用 CreateUserRequest 方法将用户添加到您的账号。

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);
    }
  }

查看有权访问您账号的用户

如需查看对您的账号拥有访问权限的所有用户,请使用 accounts.users.list 方法。

以下示例演示了如何使用 ListUsersRequest 方法列出给定账号的所有用户。

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);
    }
  }

检索指定账号的单个用户

以下示例演示了如何使用 GetUserRequest 方法检索给定账号的用户。

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);
    }
  }

从指定账号中移除用户

如需从账号中移除用户,请使用 accounts.users.delete 方法发出请求。

以下示例演示了如何使用 DeleteUserRequest 方法从给定账号中移除用户。

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);
    }
  }

更改用户的访问权限级别

如需更改用户的访问权限级别,请使用新访问权限级别调用 accounts.users.patch 方法。

以下示例演示了如何使用 UpdateUserSample 方法更新用户,使其成为给定账号的管理员。

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);
    }
  }

超级用户、管理员和标准访问权限的比较

您无法从 Merchant Center 中移除对商家管理工具拥有超级用户访问权限的用户。如需详细了解超级用户访问权限,请参阅以超级用户的身份管理您的商家

某些方法(例如用于写入账号数据的方法)需要管理员访问权限。如需了解每种方法所需的访问权限级别,请参阅参考文档

如果未指定访问权限级别,您可以使用具有标准访问权限的方法。

后续步骤