POST https://admin.googleapis.com/admin/directory/v1/users 403 Forbidden

private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";

private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

public static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, final List<String> SCOPES)
    throws IOException {
InputStream in = ClassroomQuickstart.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));

GoogleAuthorizationCodeFlow.Builder builder = new GoogleAuthorizationCodeFlow.Builder(
    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES);

GoogleAuthorizationCodeFlow flow = builder
    //.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(" email  ");
}

private static final List<String> SCOPES = new ArrayList<String>();
private static final int MAX_RESULTS = 500;

public static void main(String[] args) throws IOException, GeneralSecurityException {
SCOPES.addAll(DirectoryScopes.all());

final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

Credential credentials = ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES);

final Directory directory = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
    .setApplicationName(APPLICATION_NAME).build();

final User userInsert = new User();
userInsert.setChangePasswordAtNextLogin(true); // default true

final UserName name = new UserName();
name.setFullName("test testovich");
name.setFamilyName("test");  //last name 
name.setGivenName("testovich"); // first  name
userInsert.setName(name);

userInsert.setPrimaryEmail("[email protected]");
userInsert.setPassword("testovich");
userInsert.setOrgUnitPath("/");

directory.users().insert(userInsert).execute();

}

Exception in thread “main” com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
POST https://admin.googleapis.com/admin/directory/v1/users
{
“code”: 403,
“errors”: [
{
“domain”: “global”,
“message”: “Not Authorized to access this resource/api”,
“reason”: “forbidden”
}
],
“message”: “Not Authorized to access this resource/api”
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$3.interceptResponse(AbstractGoogleClientRequest.java:466)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:552)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:493)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:603)
at classroom.DirectoryQuickstart.main(DirectoryQuickstart.java:142)

  • What exactly is your question?

    – 

Leave a Comment