Box.com User CRUD API Example
The Box REST API end point Usage examples are Create User, Update User, Add Email Alias to the User, Get Email Aliases for the User, Update Primary Email Account, Remove the Existing Primary email Account as an Alias, and Remove the User from the box.comPre-Requisite
1. Download and Install Gradle
Creating Eclipse Project Using Gradle
1. Set the Java Home Environment Variable
export JAVA_HOME=/usr/lib/jvm/java
2. Set the Gradle Home Environment Variable
export GRADLE_HOME=/home/gradle/gradle-1.10
3. Create the eclipse Project, and source Directory
mkdir box-api-usage
mkdir box-api-usage/src
mkdir box-api-usage/src/main
mkdir box-api-usage/src/main/java
mkdir box-api-usage/src/main/resources
4. Create the build.gradle file in the box-api-usage directory
apply plugin: 'application'
apply plugin: 'eclipse'
repositories{
mavenCentral()
}
dependencies{
compile 'org.apache.httpcomponents:httpclient:4.2.2'
compile 'org.slf4j:slf4j-api:1.7.5','org.slf4j:slf4j-log4j12:1.7.5'
compile 'com.google.code.gson:gson:2.2.2'
}
mainClassName='box.api.usage.client.BoxAPIUsage'
5. Create the Eclipse Project
gradle cleanEclipse eclipse
6. Import the Project into the Eclipse
File --> Import --> Existing Project Into Workspace --> Next -->Select box-api-usage directory and click finish. It will create the eclipse project directory.
Get the Access Token from Box.com
Convert the Access Token to Map Object
GenerateBoxCode accessCode= new GenerateBoxCode();String accessToken=accessCode.getAccessToken();
Map<String, Object> tokenMap= null;
Gson gson= new Gson();
tokenMap=gson.fromJson(accessToken, Map.class);
String accessToken= tokenMap.get("access_token");
System.out.println(tokenMap);
Create User in Box.com
1. Build Create Request Attributes
String emailId="<Replace With Your Email Id>";
String displayName="<Replace With Your Display Name>";
String role="<Replace With your Role>";
String id="";
Gson gson= new Gson();
Map<String, String> createRequest= new HashMap<String,String>();
createRequest.put("login", emailId);
createRequest.put("name", displayName);
createRequest.put("role", role);
2. Convert Map object to Json Object
String jsonCreateRequest= gson.toJson(createRequest);
3. Send the Post Request to create the User in Box.com
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost(provisionEndPoint);
post.addHeader("Authorization", "Bearer " + accessToken);
try
{
post.setEntity(new StringEntity(jsonCreateRequest));
HttpResponse response= client.execute(post);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Creation Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Update User in Box.com
displayName="<Replace With Your Display Name>";
Map<String, String> updateRequest= new HashMap<String,String>();
updateRequest.put("name", displayName);
2. Convert Map object to Json Object
String jsonUpdateRequest= gson.toJson(updateRequest);
3. Send the PUT Request to create the User in Box.com
HttpClient client= new DefaultHttpClient();
HttpPut put= new HttpPut(provisionEndPoint+"/"+id);
put.addHeader("Authorization", "Bearer " + accessToken);
try
{
put.setEntity(new StringEntity(jsonUpdateRequest));
HttpResponse response= client.execute(put);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Update Failed Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Add Aliases for the User in Box.com
String aliasName="<Replace With your Alias Name>";
String aliasId="";
Map<String, String> addEmailAlias= new HashMap<String,String>(); addEmailAlias.put("email", aliasName);
2. Convert Map object to Json Object
String jsonEmailAliasRequest= gson.toJson(addEmailAlias);
3. Send the POST Request to create the User in Box.com
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost(provisionEndPoint+"/"+id+"/email_aliases");
post.addHeader("Authorization", "Bearer " + accessToken);
try
{
post.setEntity(new StringEntity(jsonEmailAliasRequest));
HttpResponse response= client.execute(post);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
aliasId=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Add Alias Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Update Primary Email User in Box.com
1. Build Update Request Attributes
Map<String, String> updateEmail= new HashMap<String,String>(); updateEmail.put("login", aliasName);
2. Convert Map object to Json Object
String jsonEmailUpdateRequest= gson.toJson(updateEmail);
3. Send the PUT Request to update the User in Box.com
HttpClient client= new DefaultHttpClient();
HttpPut put= new HttpPut(provisionEndPoint+"/"+id);
put.addHeader("Authorization", "Bearer " + accessToken);
try
{
put.setEntity(new StringEntity(jsonEmailUpdateRequest));
HttpResponse response= client.execute(put);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Update Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Get All Email Aliases for the User in Box.com
1. Send the Get Request to Get All Email Aliases for the User in Box.com
Map<String, List<Map<String, String>>> data=null;
HttpClient client= new DefaultHttpClient();
HttpGet get= new HttpGet(provisionEndPoint+"/"+id+"/email_aliases");
get.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(get);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
data= gson.fromJson(strresponse, Map.class);
System.out.println(String.format("Raw Jason Data : %s" , data));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Get Alias Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Remove Aliases for the User in Box.com
1. Send the Delete Request to Delete the Email Alias for the User in Box.com
HttpClient client= new DefaultHttpClient();
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+id+"/email_aliases/"+aliasId);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Alias Deletion Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+id+"/email_aliases/"+aliasId);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Alias Deletion Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Remove the User from Box.com
1. Send the Delete Request to Delete the User in Box.com
HttpClient client= new DefaultHttpClient();
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+id);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Deletion Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+id);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Deletion Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
Sample Code
package box.api.usage.client;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class BoxAPIUsage {
public BoxAPIUsage() {
}
private static String emailId="<Replace Your BoxUser Login Email>";
private static String displayName="<Replace Your Display Name>";
private static String role="user";
private static String accessToken="";
private static String provisionEndPoint = "https://api.box.com/2.0/users";
private static String emailAlias="<Replacing Your Alias to modify the Primary Email Account>";
private static Gson gson= new Gson();
public static void main(String[] args) {
GenerateBoxCode accessCode= new GenerateBoxCode();
String accessToken=accessCode.getAccessToken();
Map<String, Object> tokenMap= null;
tokenMap=gson.fromJson(accessToken, Map.class);
accessToken=tokenMap.get("access_token").toString();
System.out.println(accessToken);
// Creating User
String id=createUser(accessToken);
System.out.println("Created ID :"+id);
// Updating user DsiplayName
id=updateUser(accessToken, id);
// Adding alias to the user to modify the primary email account
String aliasId=addAlias(accessToken, id);
System.out.println("Alias id :"+aliasId);
//Updating Email Account
id=updateEmailUser(accessToken, id);
// Getting the Email Aliases to remove the existing email
Map<String, List<Map<String, String>>> aliases= getAliases(accessToken, id);
if(aliases != null)
{
List<Map<String, String>> entries= aliases.get("entries");
for (Map<String, String> map : entries)
{
aliasId=map.get("id");
// removing the Email Alias
removeAlias(accessToken, id, aliasId);
}
}
// Removing the User
removeUser(accessToken, id);
}
public static String createUser(String accessToken)
{
String id="";
Map<String, String> createRequest= new HashMap<String,String>();
createRequest.put("login", emailId);
createRequest.put("name", displayName);
createRequest.put("role", role);
String jsonCreateRequest= gson.toJson(createRequest);
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost(provisionEndPoint);
post.addHeader("Authorization", "Bearer " + accessToken);
try
{
post.setEntity(new StringEntity(jsonCreateRequest));
HttpResponse response= client.execute(post);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Creation Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return id;
}
public static String updateUser(String accessToken,String boxid)
{
String id="";
displayName=displayName+ "M";
Map<String, String> updateRequest= new HashMap<String,String>();
updateRequest.put("name", displayName);
String jsonUpdateRequest= gson.toJson(updateRequest);
HttpClient client= new DefaultHttpClient();
HttpPut put= new HttpPut(provisionEndPoint+"/"+boxid);
put.addHeader("Authorization", "Bearer " + accessToken);
try
{
put.setEntity(new StringEntity(jsonUpdateRequest));
HttpResponse response= client.execute(put);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Creation Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return id;
}
public static String addAlias(String accessToken,String boxid)
{
String aliasId="";
displayName=displayName+ "M";
Map<String, String> addEmailAlias= new HashMap<String,String>();
addEmailAlias.put("email", emailAlias);
String jsonEmailAliasRequest= gson.toJson(addEmailAlias);
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost(provisionEndPoint+"/"+boxid+"/email_aliases");
post.addHeader("Authorization", "Bearer " + accessToken);
try
{
post.setEntity(new StringEntity(jsonEmailAliasRequest));
HttpResponse response= client.execute(post);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
aliasId=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Update Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return aliasId;
}
public static Map<String, List<Map<String, String>>> getAliases(String accessToken,String boxid)
{
Map<String, List<Map<String, String>>> data=null;
HttpClient client= new DefaultHttpClient();
HttpGet get= new HttpGet(provisionEndPoint+"/"+boxid+"/email_aliases");
get.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(get);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
data= gson.fromJson(strresponse, Map.class);
System.out.println(String.format("Raw Jason Data : %s" , data));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Get Aliases Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return data;
}
public static void removeAlias(String accessToken,String boxid,String aliasId)
{
HttpClient client= new DefaultHttpClient();
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+boxid+"/email_aliases/"+aliasId);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Remove Alias Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
}
public static String updateEmailUser(String accessToken,String boxid)
{
String id="";
displayName=displayName+ "M";
Map<String, String> updateEmail= new HashMap<String,String>();
updateEmail.put("login", emailAlias);
String jsonEmailUpdateRequest= gson.toJson(updateEmail);
HttpClient client= new DefaultHttpClient();
HttpPut put= new HttpPut(provisionEndPoint+"/"+boxid);
put.addHeader("Authorization", "Bearer " + accessToken);
try
{
put.setEntity(new StringEntity(jsonEmailUpdateRequest));
HttpResponse response= client.execute(put);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Update Email Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return id;
}
public static void removeUser(String accessToken,String boxid)
{
HttpClient client= new DefaultHttpClient();
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+boxid);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Deletion Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class BoxAPIUsage {
public BoxAPIUsage() {
}
private static String emailId="<Replace Your BoxUser Login Email>";
private static String displayName="<Replace Your Display Name>";
private static String role="user";
private static String accessToken="";
private static String provisionEndPoint = "https://api.box.com/2.0/users";
private static String emailAlias="<Replacing Your Alias to modify the Primary Email Account>";
private static Gson gson= new Gson();
public static void main(String[] args) {
GenerateBoxCode accessCode= new GenerateBoxCode();
String accessToken=accessCode.getAccessToken();
Map<String, Object> tokenMap= null;
tokenMap=gson.fromJson(accessToken, Map.class);
accessToken=tokenMap.get("access_token").toString();
System.out.println(accessToken);
// Creating User
String id=createUser(accessToken);
System.out.println("Created ID :"+id);
// Updating user DsiplayName
id=updateUser(accessToken, id);
// Adding alias to the user to modify the primary email account
String aliasId=addAlias(accessToken, id);
System.out.println("Alias id :"+aliasId);
//Updating Email Account
id=updateEmailUser(accessToken, id);
// Getting the Email Aliases to remove the existing email
Map<String, List<Map<String, String>>> aliases= getAliases(accessToken, id);
if(aliases != null)
{
List<Map<String, String>> entries= aliases.get("entries");
for (Map<String, String> map : entries)
{
aliasId=map.get("id");
// removing the Email Alias
removeAlias(accessToken, id, aliasId);
}
}
// Removing the User
removeUser(accessToken, id);
}
public static String createUser(String accessToken)
{
String id="";
Map<String, String> createRequest= new HashMap<String,String>();
createRequest.put("login", emailId);
createRequest.put("name", displayName);
createRequest.put("role", role);
String jsonCreateRequest= gson.toJson(createRequest);
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost(provisionEndPoint);
post.addHeader("Authorization", "Bearer " + accessToken);
try
{
post.setEntity(new StringEntity(jsonCreateRequest));
HttpResponse response= client.execute(post);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Creation Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return id;
}
public static String updateUser(String accessToken,String boxid)
{
String id="";
displayName=displayName+ "M";
Map<String, String> updateRequest= new HashMap<String,String>();
updateRequest.put("name", displayName);
String jsonUpdateRequest= gson.toJson(updateRequest);
HttpClient client= new DefaultHttpClient();
HttpPut put= new HttpPut(provisionEndPoint+"/"+boxid);
put.addHeader("Authorization", "Bearer " + accessToken);
try
{
put.setEntity(new StringEntity(jsonUpdateRequest));
HttpResponse response= client.execute(put);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Creation Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return id;
}
public static String addAlias(String accessToken,String boxid)
{
String aliasId="";
displayName=displayName+ "M";
Map<String, String> addEmailAlias= new HashMap<String,String>();
addEmailAlias.put("email", emailAlias);
String jsonEmailAliasRequest= gson.toJson(addEmailAlias);
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost(provisionEndPoint+"/"+boxid+"/email_aliases");
post.addHeader("Authorization", "Bearer " + accessToken);
try
{
post.setEntity(new StringEntity(jsonEmailAliasRequest));
HttpResponse response= client.execute(post);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
aliasId=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Update Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return aliasId;
}
public static Map<String, List<Map<String, String>>> getAliases(String accessToken,String boxid)
{
Map<String, List<Map<String, String>>> data=null;
HttpClient client= new DefaultHttpClient();
HttpGet get= new HttpGet(provisionEndPoint+"/"+boxid+"/email_aliases");
get.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(get);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
data= gson.fromJson(strresponse, Map.class);
System.out.println(String.format("Raw Jason Data : %s" , data));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Get Aliases Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return data;
}
public static void removeAlias(String accessToken,String boxid,String aliasId)
{
HttpClient client= new DefaultHttpClient();
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+boxid+"/email_aliases/"+aliasId);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Remove Alias Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
}
public static String updateEmailUser(String accessToken,String boxid)
{
String id="";
displayName=displayName+ "M";
Map<String, String> updateEmail= new HashMap<String,String>();
updateEmail.put("login", emailAlias);
String jsonEmailUpdateRequest= gson.toJson(updateEmail);
HttpClient client= new DefaultHttpClient();
HttpPut put= new HttpPut(provisionEndPoint+"/"+boxid);
put.addHeader("Authorization", "Bearer " + accessToken);
try
{
put.setEntity(new StringEntity(jsonEmailUpdateRequest));
HttpResponse response= client.execute(put);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 200 || statusCode == 201)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
Map<String, Object> data= gson.fromJson(strresponse, Map.class);
id=data.get("id").toString();
System.out.println(String.format("Raw Jason Data : %s" , strresponse));
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Update Email Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
return id;
}
public static void removeUser(String accessToken,String boxid)
{
HttpClient client= new DefaultHttpClient();
HttpDelete delete= new HttpDelete(provisionEndPoint+"/"+boxid);
delete.addHeader("Authorization", "Bearer " + accessToken);
try
{
HttpResponse response= client.execute(delete);
int statusCode=response.getStatusLine().getStatusCode();
System.out.println(String.format("Response Status Code : %d" , statusCode));
if( statusCode == 204)
{
HttpEntity entity=response.getEntity();
if (entity != null) {
BasicResponseHandler handler= new BasicResponseHandler();
String strresponse =handler.handleResponse(response);
System.out.println(strresponse);
EntityUtils.consume(entity);
}
}
else
{
System.out.println("User Deletion Error");
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(client != null)
{
client.getConnectionManager().shutdown();
}
}
}
}
No comments:
Post a Comment