I have been able to use google admin api to login to google apps and retrieve user list. I need to do simiar using HTTPClient. I have earlier created a service account and been able to get the access token using JWT approach. Had granted authorization rights to scope using admin console advance secuurity settings.
I need to use this access token to create/update/read users. Despite having authorization requests for the give service account (thats how i was able to get the token) i am getting forbidden error.
{
"domain": "global",
"reason": "forbidden",
"message": "Not Authorized to access this resource/api"
}
],
"code": 403,
"message": "Not Authorized to access this resource/api"
}
}
I have checked this access token against
curl https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#access_token
and see that it is valid token.
Sample Java Snippet
public void createUser() {
String params="{"
+"\"name\": {"
+"\"familyName\": \"Smith\","
+"\"givenName\": \"John\","
+"\"fullName\": \"John Smith\""
+"},"
+"\"password\": \"<some password>\","
+"\"primaryEmail\": \"john.smith@xyz.net\","
+"\"isAdmin\": false,"
+"\"isDelegatedAdmin\": false,"
+"\"isMailboxSetup\": true"
+"}";
PostMethod method =null;
try {
JSONObject json=new JSONObject(params);
String url="https://www.googleapis.com/admin/directory/v1/users";
//+ "?access_token="+ accessToken;
method = new PostMethod(url);
method.addRequestHeader("Content-Type", "application/json");
method.addRequestHeader("Authorization","Bearer " + accessToken);
method.setRequestEntity(new StringRequestEntity(json.toString(),
"application/json", null));
method.execute();
System.out.println(method.getResponseBodyAsString());
if (method.getStatusCode() == HttpStatus.SC_CREATED) {
try {
JSONObject response = new JSONObject(method.getResponseBodyAsString());
if (response.getBoolean("success")) {
System.out..println( "User Account created Successfully. <br>");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
method.releaseConnection;
}
return null;
}