ควบคุมการเข้าถึงบัญชีของคุณ

คุณสามารถใช้ Merchant Accounts API เพื่อควบคุมผู้ที่มีสิทธิ์เข้าถึงบัญชีผู้ขายและระดับการเข้าถึงของผู้ขาย

User คือผู้ที่มีสิทธิ์เข้าถึงบัญชีผู้ขาย คุณสามารถใช้ Merchant Account API เพื่อดู เพิ่ม และนำผู้ใช้ออกจากบัญชีได้

เพิ่มผู้ใช้ในบัญชี

หากต้องการเพิ่มผู้ใช้ ให้เรียกใช้เมธอด accounts.users.create และระบุระดับการเข้าถึงในช่อง access_rights ของ User

ตัวอย่างคำขอต่อไปนี้แสดงวิธีเพิ่มผู้ใช้ใหม่ที่มีสิทธิ์ STANDARD และ PERFORMANCE_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);
    }
  }

การเปรียบเทียบระหว่างสิทธิ์เข้าถึงระดับผู้ดูแลระบบขั้นสูง ผู้ดูแลระบบ และมาตรฐาน

คุณนำผู้ใช้ที่มีสิทธิ์เข้าถึงระดับผู้ดูแลระบบขั้นสูงใน Business Manager ออกจาก Merchant Center ไม่ได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับสิทธิ์เข้าถึงระดับผู้ดูแลระบบขั้นสูงได้ที่จัดการธุรกิจในฐานะผู้ดูแลระบบขั้นสูง

วิธีการบางอย่าง เช่น วิธีการเขียนข้อมูลบัญชี ต้องใช้สิทธิ์เข้าถึงระดับผู้ดูแลระบบ หากต้องการดูระดับการเข้าถึงที่จําเป็นสําหรับแต่ละวิธี โปรดดูเอกสารอ้างอิง

หากไม่ได้ระบุระดับการเข้าถึง คุณจะใช้เมธอดดังกล่าวด้วยการเข้าถึงมาตรฐานได้

ขั้นตอนถัดไป