Create gmail account for a domain using Google Admin SDK Directory API in Java

雨燕双飞 提交于 2020-01-20 05:26:14

问题


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:


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("jo.blogs@example.com");

        // 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



来源:https://stackoverflow.com/questions/25383591/create-gmail-account-for-a-domain-using-google-admin-sdk-directory-api-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!