2

How to create a gmail account for a domain in Java using the Google Admin SDK Directory API? Is there an example somewhere? The Google documentation is terrible regarding this issue.

Marcos

1 Answer 1

5

This is a basic example showing how to create a user using the Directory API of the Admin SDK (https://developers.google.com/admin-sdk/directory/v1/reference/users/insert). I’ve not included the OAuth 2 initialization code as this is fairly standard and similar to other Google APIs. You need to initialize a Directory instance using your OAuth credential and this will depend on the type of application you are using (standalone or App Engine), have a look here (https://code.google.com/p/google-api-java-client/source/browse/?repo=samples) there are many examples for other APIs covering both App Engine and standalone apps. The OAuth credentials initialization should be very similar in this case.

You need to download the latest Admin SDK jar from here (https://developers.google.com/api-client-library/java/apis/admin/directory_v1) or include the following maven dependency

<dependency>
     <groupId>com.google.apis</groupId>
     <artifactId>google-api-services-admin</artifactId>
     <version>directory_v1-rev32-1.16.0-rc</version> 
</dependency>

Here is the Java example

import java.io.IOException;
import java.security.GeneralSecurityException;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.model.User;
import com.google.api.services.admin.directory.model.UserName;

public class DirectoryUtils {

    /** Global instance of the JSON factory. */
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    /** Global instance of the HTTP transport. */
    private static HttpTransport httpTransport;

    public static Directory initDirectory() throws GeneralSecurityException, IOException {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        Credential credential = null; // TODO initialize credentials

        Directory directory = new Directory.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName("My App Name")
        .build();

        return directory;
    }

    public static User createUser(Directory directory) throws IOException {
        User user = new User();
        // populate are the required fields only
        UserName name = new UserName();
        name.setFamilyName("Blogs");
        name.setGivenName("Jo");
        user.setName(name);
        user.setPassword("password101");
        user.setPrimaryEmail("[email protected]");

        // requires DirectoryScopes.ADMIN_DIRECTORY_USER scope  
        user = directory.users().insert(user).execute();

        return user;
    }
}

Important

You need to ensure the OAuth tokens are obtained by someone who have domain access and can create domain users from the Admin Panel. You also need to ensure your Client Id has access to the following scope (https://www.googleapis.com/auth/admin.directory.user). To add this navigate to the admin Panel → More Controls → Security → Advanced Settings → Manage OAuth Client access, then enter your client id and the above scope

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I'll have a look at this.
Sorry for the dumb question, but as I'm new to all this, what's the application name in the Directory.Builder? Is is just an arbitrary name that I can provide or a real application name? If it is a real application name, what application is this and from where it come from? Do I have to create and register an application somewhere?
I think it's used by Google for logging purposes so any string would do
In my Admin Panel there are only two icons: Users and Groups. The More Controls option is empty, so there's no Security icon there. I don't know why. Do I have to add some configuration for the Security option to appear? How do I add it to my Admin Panel?
In the Google Developers Console I generated and API KEY for public api access. This api key is 39 characters long. Would you help me in the process of how to create the .p12 file that refers to this key? I'm having trouble trying to achieve this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.