I'm trying to update a user with an externalId based on the Google Admin SDK documentation.
UserExternalId externalId = new UserExternalId();
externalId.setType( "account" );
externalId.setValue( "test" );
User user = new User();
user.setExternalIds( externalId );
try {
User update = directory.users().update( "USERKEY", user ).execute().setExternalIds( externalId );
LOGGER.info("Response from google: " + update.toPrettyString());
User full = directory.users().get( "USERKEY" ).setProjection( "full" ).execute();
LOGGER.info( "Response from new get user: " + full.toPrettyString() );
} catch (IOException e) {
LOGGER.info("Error: " + e);
}
When logging the response on the update call I can see that externalId is filled in and no errors are thrown. When I try to get the same user there is no trace of the externalId.
When I use Google' APIs explorer and fill in the ExternalId there I get the same behavior. It looks like the Google API is accepting the update request but is ignoring the ExternalId. What is the correct way to add an externalId to a user?
edit:
SGC's answer helped me out. The setExternalIds method expects a list of ExternalIds, I forgot to do this. The Google Java Directory API seems to return a json object when fetching externalIds, so it is necessary to parse it to read it.
When updating external id of user, if the JSON is not set properly, it won't be saved(though it gives 200 response). If you try to send get request on this user, then the updated value is not seen in the response.
Try sending the request using "Oauth Play ground" , where you can build your JSON while sending request.
I tried using {"externalIds":[{"value":"shaggynetwork","type":"network"}]} in "Users.update" request. And in the "Users.get", the response is:
HTTP/1.1 200 OK
Content-type: application/json; charset=UTF-8
{
"externalIds": [
{
"type": "network",
"value": "shaggynetwork"
}
],
Steps:
In the step 1, on the left side of "Authorize APIs" in the blank give https://www.googleapis.com/auth/admin.directory.user, then click on "Authorize APIs".
3.In step 2, click on "exchange code for tokens"
- In step 3, select HTTP method:PUT, give request URI value:https://www.googleapis.com/admin/directory/v1/users/userKey(giver userkey value).
5.click on "enter request body", build json:
{"externalIds":[{"value":"shaggynetwork","type":"network"}]}
6.CLICK on "send request".
In documentation, its not clear on how to update external id.
Let me know if you need any help on this.
来源:https://stackoverflow.com/questions/27860653/how-to-update-a-users-externalid-using-java