Se hai accesso a un utente, aggiorna direttamente i suoi ruoli. Aggiorna i ruoli utente assegnati a un utente utilizzando una richiesta bulkEditAssignedUserRoles.
Per concedere a un utente l'accesso Sola lettura per un insieme di inserzionisti:
Java
// Provide the ID of the user to update permissions for. long userId = user-id; // Provide the IDs of the advertisers to provide the user Read only access // to. List<Long> advertiserIds = advertiser-ids; // Retrieve the existing user. User existingUser = service.users().get(userId).execute(); // Identify advertiser permissions that need to be removed and build list of // delete requests. List<String> deletedUserRoles = new ArrayList<>(); for (AssignedUserRole userRole : existingUser.getAssignedUserRoles()) { if (userRole.getAdvertiserId() != null && advertiserIds.contains(userRole.getAdvertiserId())) { deletedUserRoles.add("advertiser-" + userRole.getAdvertiserId()); } } // Build list of add requests. List<AssignedUserRole> addedUserRoles = new ArrayList<>(); for (long id : advertiserIds) { addedUserRoles.add(new AssignedUserRole().setAdvertiserId(id).setUserRole("READ_ONLY")); } // Create a bulk edit request body. BulkEditAssignedUserRolesRequest requestContent = new BulkEditAssignedUserRolesRequest() .setCreatedAssignedUserRoles(addedUserRoles) .setDeletedAssignedUserRoles(deletedUserRoles); // Configure the bulk edit request. Users.BulkEditAssignedUserRoles request = service.users().bulkEditAssignedUserRoles(userId, requestContent); // Execute bulk edit request. BulkEditAssignedUserRolesResponse response = request.execute(); // Check if response is empty. // If not, iterate over created assigned user roles. if (response.isEmpty()) { System.out.print("Bulk edit request created no new assigned user roles"); } else { System.out.println("The bulk edit request created the following user roles:"); for (AssignedUserRole assignedUserRole : response.getCreatedAssignedUserRoles()) { System.out.printf( "Assigned user role %s was created%n", assignedUserRole.getAssignedUserRoleId()); } }
Python
# Provide the ID of the user to update permissions for. user_id = user-id # Provide the IDs of the advertisers to provide the user Read only access # to. advertiser_ids = advertiser-ids # Retrieve the existing user. existing_user = service.users().get(userId=user_id).execute() # Identify advertiser permissions that need to be removed and build list of # delete requests. deleted_user_roles = [] for role in existing_user["assignedUserRoles"]: # Check if the role is for an advertiser ID that is being added. if "advertiserId" in role and role["advertiserId"] in advertiser_ids: deleted_user_roles.append(f'advertiser-{role["advertiserId"]}') # Build list of add requests. added_user_roles = [] for id in advertiser_ids: added_user_roles.append({"advertiserId": id, "userRole": "READ_ONLY"}) # Build the bulk edit request. bulk_edit_user_access_request = { "createdAssignedUserRoles": added_user_roles, "deletedAssignedUserRoles": deleted_user_roles, } # Edit the assigned user roles. response = ( service.users() .bulkEditAssignedUserRoles( userId=user_id, body=bulk_edit_user_access_request ) .execute() ) # Print the created user roles. if ( "createdAssignedUserRoles" in response and len(response["createdAssignedUserRoles"]) != 0 ): print("The bulk edit request created the following user roles:") for role in response["createdAssignedUserRoles"]: print(f'ID: {role["assignedUserRoleId"]}, Role: {role["userRole"]}') else: print("No user roles were successfully created by the bulk edit request.")
PHP
// Provide the ID of the user to update permissions for. $userId = user-id; // Provide the IDs of the advertisers to provide the user Read only access // to. $advertiserIds = advertiser-ids; // Retrieve the existing user. try { $response = $this ->service ->users ->get($userId); } catch (\Exception $e) { $this->renderError($e); return; } $existingUserRoles = $response->getAssignedUserRoles(); // Identify advertiser permissions that need to be removed and build list of // delete requests. $deletedUserRoles = array(); foreach ($existingUserRoles as $userRole) { if (!empty($userRole->getAdvertiserId()) && $userRole->getAdvertiserId() == $advertiserId) { $deletedUserRoles[] = "advertiser-" . $advertiserId; } } // Build list of add requests. $addedUserRoles = array(); foreach ($advertiserIds as $id) { $newRole = new Google_Service_DisplayVideo_AssignedUserRole(); $newRole->setAdvertiserId($id); $newRole->setUserRole("READ_ONLY"); $addedUserRoles[] = $newRole; } // Create and configure the bulk edit request body. $body = new Google_Service_DisplayVideo_BulkEditAssignedUserRolesRequest(); $body->setCreatedAssignedUserRoles($addedUserRoles); $body->setDeletedAssignedUserRoles($deletedUserRoles); // Call the API, editing the assigned user roles for the identified // user. try { $response = $this ->service ->users ->bulkEditAssignedUserRoles( $userId, $body ); } catch (\Exception $e) { $this->renderError($e); return; } // Check if response is empty. // If not, iterate over created assigned user roles. if (!empty($response)) { print('<p>The bulk edit request created the following user roles:</p><ul>'); $createdUserRoles = $response->getCreatedAssignedUserRoles(); foreach ($createdUserRoles as $addedRole) { printf('<li>Assigned user role %s was created</li>', $addedRole->getAssignedUserRoleId()); } print('</ul>'); } else { print('<p>Bulk edit request created no new assigned user roles</p>'); }