使用Java中的Google Admin SDK Directory API为域创建Gmail帐户 [英] Create gmail account for a domain using Google Admin SDK Directory API in Java

查看:136
本文介绍了使用Java中的Google Admin SDK Directory API为域创建Gmail帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Google Admin SDK目录API在Java中为域创建Gmail帐户?那里有个例子吗?关于此问题,Google文档非常糟糕.

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.

马科斯

推荐答案

这是一个基本示例,显示了如何使用Admin SDK( https://code.google.com/p/google-api-java-client/source/browse/? repo = samples ),还有很多其他API的示例,涵盖了App Engine和独立应用程序.在这种情况下,OAuth凭证初始化应该非常相似.

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.

您需要从此处下载最新的Admin SDK jar( https://developers.google.com/api-client-library/java/apis/admin/directory_v1 )或包含以下Maven依赖项

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>

这是Java示例

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;
    }
}

重要

您需要确保OAuth令牌是由具有域访问权限并可以从管理面板创建域用户的人获取的.您还需要确保您的客户ID可以访问以下范围( https://www. googleapis.com/auth/admin.directory.user ).要添加此内容,请导航到管理面板→更多控件→安全性→高级设置→管理OAuth客户端访问权限,然后输入您的客户端ID和上述范围

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

这篇关于使用Java中的Google Admin SDK Directory API为域创建Gmail帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆