變更使用者存取權

建立時,使用者必須至少具備一個指派的使用者角色。不過, 新建廣告主或將機構組織擴展到多個夥伴、使用者 存取權必須更新,納入新的相關資源。

使用者角色必須透過 users.bulkEditAssignedUserRoles 方法,需要 就能透過單一動作移除及新增使用者角色。您可以藉此變更 對單一要求的使用者存取層級。

以下範例說明如何編輯指派的使用者角色 現有使用者的特徵:

Java

// Retrieve the existing user.
User user = service.users().get(user-id).execute();

// Create the bulk edit request structure.
BulkEditAssignedUserRolesRequest bulkEditRequest =
    new BulkEditAssignedUserRolesRequest();

// Build list of user roles to add.
ArrayList<AssignedUserRole> addedUserRoles =
    new ArrayList<AssignedUserRole>();

// Add the user role for the new advertiser and the new user role
// for the existing advertiser to assign to the user.
addedUserRoles.add(new AssignedUserRole()
    .setAdvertiserId(new-advertiser-id)
    .setUserRole("STANDARD"));
addedUserRoles.add(new AssignedUserRole()
    .setAdvertiserId(existing-advertiser-id)
    .setUserRole("READ_ONLY"));

// Add list of user roles to add to the request.
bulkEditRequest.setCreatedAssignedUserRoles(addedUserRoles);

// Build list of user role IDs to delete.
ArrayList<String> deletedUserRoles = new ArrayList<String>();

// Create assigned user role ID to check for.
String existingAssignedUserRoleId = String.format(
    "advertiser-%d",
    existing-advertiser-id
);

// Check the existing user roles. If the user has an existing user role
// for the existing advertiser, add it to the list of roles to be deleted.
// Users cannot have multiple roles for a single resource.
for (AssignedUserRole userRole: user.getAssignedUserRoles()) {
  if (userRole.getAssignedUserRoleId() == existingAssignedUserRoleId) {
    deletedUserRoles.add(existingAssignedUserRoleId);
  }
}

// Add list of user roles to delete to the request.
bulkEditRequest.setDeletedAssignedUserRoles(deletedUserRoles);

// Build and execute the bulk edit request.
BulkEditAssignedUserRolesResponse response = service.users()
    .bulkEditAssignedUserRoles(
        user-id,
        bulkEditRequest
    ).execute();

// Check if response is empty.
// If not, iterate over created AssignedUserRoles.
if (response.isEmpty()) {
  System.out.print("Bulk edit request created no new AssignedUserRoles.");
} else {
  for (AssignedUserRole assignedRole : response.getCreatedAssignedUserRoles()) {
    System.out.printf("AssignedUserRole %s was created.\n",
        assignedRole.getAssignedUserRoleId());
  }
}

Python

# Retrieve the existing user.
user = service.users().get(userId=user-id).execute()

# Add the user role for the new advertiser and the new user role
# for the existing advertiser to assign to the user.
added_user_roles = [
    {
        'advertiser_id': new-advertiser-id,
        'user_role': 'STANDARD'
    },
    {
        'advertiser_id': existing-advertiser-id,
        'user_role': 'READ_ONLY'
    }
]

# Create empty deleted user role list to add to if necessary.
deleted_user_roles = []

# Check the existing user roles. If the user has an existing user role
# for the existing advertiser, add it to the list of roles to be deleted.
# Users cannot have multiple roles for a single resource.
for role in user['assignedUserRoles']:
  if role['assignedUserRoleId'] == ("advertiser-%s" % existing-advertiser-id):
    deleted_user_roles.append(role['assignedUserRoleId'])

# Create the bulk edit request.
bulk_edit_user_roles_request = {
    'deletedAssignedUserRoles': deleted_user_roles,
    'createdAssignedUserRoles': added_user_roles
}

# Edit the assigned user roles.
response = service.users().bulkEditAssignedUserRoles(
    userId=user-id,
    body=bulk_edit_user_roles_request
).execute()

# Check if response is empty.
# If not, iterate over and display new assigned targeting options.
if not response:
  print("Bulk edit request created no new AssignedUserRoles.")
else:
  for assigned_user_role in response['createdAssignedUserRoles']:
    print("AssignedUserRole %s was created.\n"
          % assigned_user_role['assignedUserRoleId'])

PHP

// Call the API, retrieving the existing user.
$user = $this->service->users->get(user-id);

// Create an array for user roles to add.
$addedUserRoles = array();

// Create new user role for new advertiser.
$newAdvertiserUserRole = new Google_Service_DisplayVideo_AssignedUserRole();
$newAdvertiserUserRole->setAdvertiserId(new-advertiser-id);
$newAdvertiserUserRole->setUserRole('STANDARD');
$addedUserRoles[] = $newAdvertiserUserRole;

// Create new user role for existing advertiser.
$existingAdvertiserUserRole =
    new Google_Service_DisplayVideo_AssignedUserRole();
$existingAdvertiserUserRole->setAdvertiserId(
    existing-advertiser-id
);
$existingAdvertiserUserRole->setUserRole('READ_ONLY');
$addedUserRoles[] = $existingAdvertiserUserRole;

// Create an array for user roles to delete.
$deletedUserRoles = array();

// Add user role to delete list if it is being replaced by a new user role.
foreach ($user->getAssignedUserRoles() as $role) {
    if ($role->getAdvertiserId() == existing-advertiser-id) {
        $deletedUserRoles[] = $role->getAssignedUserRoleId();
    }
}

// Create the bulk edit request.
$bulkEditUserRolesRequest =
    new Google_Service_DisplayVideo_BulkEditAssignedUserRolesRequest();
$bulkEditUserRolesRequest->setCreatedAssignedUserRoles($addedUserRoles);
$bulkEditUserRolesRequest->setDeletedAssignedUserRoles($deletedUserRoles);

// Call the API, editing the assigned user roles for the given user.
$response = $this->service->users->bulkEditAssignedUserRoles(
    user-id,
    $bulkEditUserRolesRequest
);

// Print created assigned user roles.
if (!empty($response->getCreatedAssignedUserRoles())) {
    foreach ($response->getCreatedAssignedUserRoles() as $role) {
        printf(
            'AssignedUserRole %s was created.\n',
            $role['assignedUserRoleId']
        );
    }
} else {
    print('Bulk edit request created no new AssignedUserRoles.');
}