我尝试使用以下代码通过Google Directory API创建新的用户帐户。我得到的结果是密码无效。什么密码?我下载的P12文件有问题吗?
Collection<String> SCOPE = Arrays.asList("https://www.googleapis.com/auth/admin.directory.user");
String serviceAcctEmailAddress = "xxx@developer.gserviceaccount.com";
String serviceAcctUser = "admin@x.com";
final HttpTransport TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAcctEmailAddress)
.setServiceAccountUser(serviceAcctUser)
.setServiceAccountScopes(SCOPE)
.setServiceAccountPrivateKeyFromP12File(new File("1fc6.p12"))
.build();
Directory directory = new Directory.Builder(TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("API-Project")
.build();
// create user object
User u = new User();
UserName un = new UserName();
un.setGivenName(".");
un.setFamilyName("x@x.com");
u.setName(un);
u.setPassword("Axxx1234");
u.setHashFunction("SHA-1");
u.setPrimaryEmail("x@x.com");
u.setSuspended(false);
u.setAgreedToTerms(true);
Directory.Users.Insert addUser = directory.users().insert(u);
addUser.execute();
获得以下错误:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Password",
"reason" : "invalid"
} ],
"message" : "Invalid Password"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
感谢任何帮助入门!
答案 0 :(得分:1)
确认您为用户设置的密码符合your domain's password policy。
答案 1 :(得分:0)
看来我将哈希函数设置为SHA-1格式,但我没有以该格式发送密码。一旦我删除了哈希函数,它就开始正常工作了。