I'm going to apologize in advance that I'm likely missing a few pieces of information to let anyone know that my settings are correct. Please let me know what else I need to specify. I work at a university where we host student email accounts through Google. It happens pretty frequently that they forget passwords and have to have it reset. We have a page that can set their password if they validate enough information about themselves. The code we've been using has been deprecated by Google in favor of their Directory API. I've been tasked with converting this old code:
//changes a password
@Override
public void reset ( final String username, final String password ) throws ResetException
{
try
{
Validate.notEmpty( username );
Validate.notEmpty( password );
final AppsForYourDomainClient client = ClientFactory.getClient(); //admin-user account
final UserEntry entry = client.retrieveUser( username );
Validate.isTrue( !entry.getLogin().getSuspended(), "Account is suspended." );
entry.getLogin().setPassword( password );
client.updateUser( username, entry );
}
catch ( final Exception e )
{
throw new ResetException( e );
}
}
into something using their new API. I've read a lot of the documentation and several examples, but none of them have seemed to help. I've enabled Admin SDK to our admin account through their Admin Console and registered my app and gotten a key from their Developer Console, but I can't seem to get any request to return what I want. Right now I'm just trying to get a list of users:
public void testList () throws Exception
{
InputStream is = null;
final String accessToken = getAccessToken();
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
final File p12 = new File( GoogleResetterTest.class.getClassLoader().getResource("ead05e56893a.p12").toURI() );
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId( SERVICE_ACCOUNT_EMAIL )
.setServiceAccountScopes( PlusScopes.all() )
.setServiceAccountPrivateKeyFromP12File( p12 ) //password: notasecret
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.build();
final Directory dir = new Directory.Builder( httpTransport, jsonFactory, credential)
.setApplicationName( "API Project" )
.build();
final Directory.Users diruser = dir.users();
final Directory.Users.List diruserlist = diruser.list().setDomain( EMAIL_DOMAIN );
final HttpResponse response = diruserlist.executeUsingHead();
is = response.getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
String theString = writer.toString();
IOUtils.closeQuietly( is );
}
On the diruserlist.executeUsingHead(); line I get this response:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "invalid_grant" }
To me this is a pretty useless error message because there seem to be 4 or 5 pieces that could go wrong to cause that response.
I can help thinking I'm making this whole thing too complicated. I liked the simplicity of the original code and some responses to the new API criticize that it's more complicated. Has anyone had to do this and could point me in the correct path to fix this?