I need to move an organization from one to another. I have organization units like below:
Domain.com
-> Grade 12
- Botany
- Mathematics
-> Grade 11
- Zoology
I need to move 'Botany' from Grade 12 to Grade 11. This is my code:
List<String> oua = new ArrayList<String>();
oua.add("Grade 12");
oua.add("Botany");
OrgUnit org = service.orgunits().get("my_customer", oua).execute();
org.setParentOrgUnitPath("/Grade 11");
OrgUnit org = service.orgunits().update("my_customer", oua, org).execute();
I have followed this answer. But it couldn't helpful for me. I can update the name of 'Botany'. But I couldn't move the organization unit. I am getting this error:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Parent Org unit not found",
"reason" : "notFound"
} ],
"message" : "Parent Org unit not found"
}
What causes for this error and how to solve this?
EDIT 1: Here is my full code:
public class Gsuite {
private static Logger logger = LoggerFactory.getLogger(Gsuite.class);
private static final String APPLICATION_NAME = "Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER,DirectoryScopes.ADMIN_DIRECTORY_GROUP,
DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER, DirectoryScopes.ADMIN_DIRECTORY_ORGUNIT, DirectoryScopes.ADMIN_DIRECTORY_USER_SECURITY);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
protected static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = Gsuite.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public Directory directoryApiCall() throws GeneralSecurityException, IOException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Directory service = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
return service;
}
public void updateOrgUnit() throws GeneralSecurityException, IOException {
Gsuite api = new Gsuite();
Directory service = api.directoryApiCall();
List<String> oua = new ArrayList<String>();
oua.add("Grade 12");
oua.add("Botany");
OrgUnit org = service.orgunits().get("my_customer", oua).execute();
logger.debug("Parent Organizational unit path before moving : " + org.getParentOrgUnitPath());
org.setParentOrgUnitPath("/Grade 11");
logger.debug("Parent Organizational unit path after moving : " + org.getParentOrgUnitPath());
service.orgunits().update("my_customer", oua, org).execute();
}
public static void main(String... args) throws GeneralSecurityException, IOException{
Gsuite dapi = new Gsuite();
dapi.updateOrgUnit();
}
}