简体   繁体   中英

google directory api java - Invalid Password

I'm trying to create a new user account via the Google Directory API using the code below. The result I get back is invalid password. What password? Problem with the P12 file I downloaded?

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();

Getting the below error:

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)

Thanks for any helping getting started!

确认您为用户设置的密码符合您域的密码政策

It appears I was setting the hash function to SHA-1 format but I wasn't sending the password in that format. Once I removed the hash function it started working correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM