config.clientId=<Client ID>
config.clientSecret=<Client Secret>
Box Connector 5.0 Examples
Try out the following examples using Studio or XML.
Anypoint Studio Example: Folders
This example shows how to create a folder, retrieve a folder, and delete a folder.
-
Create a new Mule project in Anypoint Studio.
-
Add the following properties to the
mule-artifact.properties
file and place it in the project’ssrc/main/resources
directory. -
Configure the HTTP Listener by adding a new HTTP global element. Click the + next to the Connector configuration field.
-
Configure the global element according to the following table:
Parameter Value Name
HTTP_Listener_config
Protocol
HTTP
Host
localhost
Port
8081
-
Drag an HTTP Listener onto the canvas and configure the following parameters:
Parameter Value Display Name
HTTP Listener
Connector configuration
Select the newly created HTTP Listener Configuration.
Path
/boxdemo
-
Set the folder properties for the folder you are going to create using a DataWeave component. Drag the DataWeave component next to the HTTP Listener and use the following script.
%dw 2.0 output application/json --- { parent: { id: 0 }, "type": "folder", name: "SampleFolder" }
-
Drag the Create folder operation of the Box Connector next to the DataWeave component to create a folder.
-
Configure Box Connector by adding a new Box global element. Click the + next to the Connector configuration field.
-
Configure the global element according to the following table.
Parameter Value Comments clientId
${config.clientId}
The client identifier as assigned by the authorization server when the client application was registered.
clientSecret
${config.clientSecret}
The client application’s client secret.
host
api.box.com
The default value. Leave as is.
port
443
The default value. Don’t change.
basePath
/2.0
The default value. Don’t change.
protocol
HTTPS
The default value. Don’t change.
localCallbackPath
/callback
The default value. Don’t change.
localCallbackConfig
HTTP_Listener_config
Select the HTTP Listener configuration created earlier.
externalCallbackUrl
http://localhost:8081/callback
Use the redirect URL that is configured in the client application.
localAuthorizationUrl
http://localhost:8081/authorize
Enter this URL after deploying the Mule application to initiate
OAUTH2
dance.authorizationUrl
https://account.box.com/api/oauth2/authorize
The authorization URL to request for an authorization code.
accessTokenUrl
https://api.box.com/oauth2/token
The access token URL to request an access token.
The corresponding XML configuration should be as follows:
<mule-box-connector:config name="Mule_box_connector_Config" doc:name="Mule-box-connector Config" property_clientId="#{config.clientId}" property_clientSecret="#{config.clientSecret}" property_localCallbackConfig="HTTP_Listener_config" property_externalCallbackUrl="http://localhost:8081/callback" />
-
In the properties editor of Box Connector, configure the parameters required for the Create folder operation:
Parameter Value Display Name
Create folder
Basic Settings
Connector configuration
Select the global Box Connector element that you created.
General
Create folder request data
#[payload]
-
Drag a Logger component to log the newly created folder ID and configure the following properties:
Parameter Value Display Name
Specifies the logger to use to log the created folder ID.
Generic
Message
Specifies the folder created with ID :
#[payload.id]
-
Drag the Get folder operation of Box Connector next to the Logger component. This retrieves the folder information and configures the following properties:
Parameter Value Display Name
Get folder
Basic Settings
Connector configuration
Select the global Box Connector element that you created.
General
Folder id
#[payload.id]
-
Drag a Logger component to log the retrieved folder information and configure the following properties:
Parameter Value Display Name
Specifies the logger that logs the retrieved folder information.
Generic
Message
#[payload]
-
Drag the Box Connector Delete folder operation to delete the folder that was just created and configure the following properties:
Parameter Value Display Name
Delete folder
Basic Settings
Connector configuration
Select the global Box Connector element that you created.
General
Folder id
#[payload.id]
-
Drag the DataWeave component to set the payload to display the result of the flow to the user, and use the following script:
%dw 2.0 output application/json --- { result : "Folder was created, retrieved and deleted successfully." }
-
Save and run the project as a Mule app.
-
Use the URL
http://localhost:8081/authorize
from a browser that you set forlocalAuthorizationUrl
in the global Box configuration element to initiate theOAUTH2
dance.This displays the page asking the Box user to either grant or deny access to the client application (which reads and writes all of files and folders stored in Box).
-
Click Grant access to Box to grant the read and write permissions.
-
Open a web browser and access
http://localhost:8081/boxdemo
.You should get a JSON response with the following content:
Result:
"Folder created, retrieved and deleted successfully."
XML Example: Demonstrate Create Folder, Retrieve Folder, and Delete Folder Operations
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:mule-box-connector="http://www.mulesoft.org/schema/mule/mule-box-connector"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mule-box-connector
http://www.mulesoft.org/schema/mule/mule-box-connector/current/mule-mule-box-connector.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config"
doc:name="HTTP Listener config">
<http:listener-connection host="localhost"
port="8081" />
</http:listener-config>
<mule-box-connector:config name="Mule_box_connector_Config"
doc:name="Mule-box-connector Config"
property_clientId="#{config.clientId}"
property_clientSecret="#{config.clientSecret}"
property_localCallbackConfig="HTTP_Listener_config"
property_externalCallbackUrl="http://localhost:8081/callback" />
<flow name="Create-Get-Delete-Folder-Flow">
<http:listener doc:name="HTTP Listener"
path="/boxdemo"
config-ref="HTTP_Listener_config"
/>
<ee:transform doc:name="DataWeave to set folder properties">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
parent: {
id: 0
},
"type": "folder",
name: "SampleFolder"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<box-connector:create-folder
doc:name="Create folder" config-ref="Mule_box_connector_Config" />
<logger level="INFO" doc:name="Logger to log the created Folder ID"
message="Folder created with ID : #[payload.id]" />
<box-connector:get-folder doc:name="Get folder"
config-ref="Mule_box_connector_Config" folder-id="#[payload.id]" />
<logger level="INFO" doc:name="Logger to log the retrieved folder info"
message="#[payload]" />
<box-connector:delete-folder
doc:name="Delete folder" config-ref="Mule_box_connector_Config"
folder-id="#[payload.id]" />
<ee:transform doc:name="DataWeave to show the result">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
result : "Folder got created, retrieved and deleted successfully"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>
Example: Upload a File
This example demonstrates how to upload files to use with other Box operations you have created.
-
Create a new Mule project in Anypoint Studio.
-
Create a folder called
input
insrc/main/resources directory
. -
Copy the file you want to upload to the
input directory
. -
Configure the HTTP Listener by adding a new HTTP global element.
-
Click the Global Elements tab at the bottom of the canvas.
-
In the Global Configuration Elements screen, click Create.
-
In the Choose Global Type wizard, expand the Connector Configuration.
-
Select HTTP Listener and click OK.
-
In the HTTP Listener screen configure the following parameters:
Parameter Value Name
HTTP_Listener_config
Protocol
HTTP
Host
localhost
Port
8081
-
Drag an HTTP Listener onto the canvas and configure the following parameters:
Parameter Value Display Name
HTTP Listener
Connector configuration
Select the HTTP Listener Configuration that has been previously created.
Path
/boxUploadFile
-
Login to Box and generate the Box developer token, which is active for 60 minutes.
-
Set this Developer Token using a DataWeave component.
-
Drag the DataWeave component next to the HTTP Listener and configure as follows.
%dw 2.0 output application/java //Set the generated developer token here var developerToken = "{developer Token}" --- "Bearer " ++ developerToken
-
Drag a Set Variable component onto the canvas and configure the following parameters:
Parameter Value Display Name
Save developer token
Name
developerToken
Value
#[payload]
-
Configure the File connector by adding a new File Global Element.
-
Click the Global Elements tab at the bottom of the canvas.
-
From the Global Configuration Elements screen, click Create.
-
From the Choose Global Type wizard, expand Connector Configuration.
-
Select File Config and click OK.
-
In the File Config screen configure the following parameter:
Parameter Value Working Directory
${mule.home}/apps/${app.name}/
-
Drag a Read File operation onto the canvas and configure the following parameters:
Parameter Value Display Name
Read File
Connector configuration
Select the File Configuration that has been previously created in global elements.
File Path
input/yourFileName
-
Transform this payload to a
multipart/form-data
type of request. -
Specify a folder to upload by setting the ID of the parent folder.
-
Drag a DataWeave component next to the Read File component and use the following script:
%dw 2.0 output multipart/form-data --- { parts : { attributes : { headers : { "Content-Type": "application/json"}, content : { "name": message.attributes.fileName , // Set the ID of the parent folder. Use "0" for the root folder. "parent": { "id":"0" } } }, file : { headers : { "Content-Disposition" : { "name": "file", "filename": message.attributes.fileName } }, content : payload } } }
-
Configure the HTTP Request by adding a new HTTP Request global element.
-
Click the Global Elements tab at the bottom of the canvas.
-
In the Global Configuration Elements screen, click Create.
-
In the Choose Global Type wizard expand Connector Configuration, select HTTP Request Configuration and click OK.
-
In the HTTP Request Configuration screen configure the following parameters:
Parameter Value Name
HTTP_Request_configuration
Base path
/
Protocol
HTTP(Default)
Host
80
Port
Use persistent connections
✓
Max Connections
-1
Connection idle timeout
30000
Response buffer size
1024
TLS configuration
None
Proxy
None
Authentication
None
Client socket properties
None
Use reconnection
unchecked
-
Drag an HTTP Request onto the canvas and configure the following parameters:
Parameter Value Display Name
Request - Box Upload File
Configuration
Select the HTTP request configuration that you created in global elements.
Method
POST
Path
URL
Body
payload
-
Create an Authorization header in the HTTP Request.
-
Click the Headers tab next to the Body in the HTTP request and click + to configure the following parameter:
Name Value "Authorization"
vars.developerToken
-
Drag the DataWeave component to set the payload to display the result of the flow to the user and configure as follows:
%dw 2.0 output application/json --- { result : "File has been successfully created" }
-
Save and run the project as a Mule application.
-
Enter the URL
http://localhost:8081/boxUploadFile
in the browser. You should get a JSON response with the following content:result : "File has been successfully created"
XML Example: Upload File
Here is the XML example to upload a file.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mule-box-connector="http://www.mulesoft.org/schema/mule/mule-box-connector" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/mule-box-connector http://www.mulesoft.org/schema/mule/mule-box-connector/current/mule-mule-box-connector.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<file:config name="File_Config" doc:name="File Config">
<file:connection workingDir="${mule.home}/apps/${app.name}/" />
</file:config>
<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" >
<http:request-connection host="80" />
</http:request-config>
<flow name="UploadFileToBox" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/boxUploadFile" />
<ee:transform doc:name="Set Developer Token Variable">
<ee:message>
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
//Set the generated developer token here
var developerToken = "{developer Token}"
---
"Bearer " ++ developerToken]]></ee:set-payload>
</ee:message>
<ee:variables>
</ee:variables>
</ee:transform>
<set-variable value="#[payload]" doc:name="Save developer token" variableName="developerToken"/>
<file:read doc:name="Read File" config-ref="File_Config" path="input/yourFileName"/>
<ee:transform doc:name="Transform to multipart/form-data">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output multipart/form-data
---
{
parts : {
attributes : {
headers : {
"Content-Type": "application/json"},
content : {
"name": message.attributes.fileName ,
// Set the ID of the parent folder. Use "0" for the root folder.
"parent": {
"id":"0"
}
}
},
file : {
headers : {
"Content-Disposition" : {
"name": "file",
"filename": message.attributes.fileName
}
},
content : payload
}
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<http:request method="POST" doc:name="Request - Box Upload File" url="https://upload.box.com/api/2.0/files/content" config-ref="HTTP_Request_configuration">
<http:headers ><![CDATA[#[output application/java
---
{
"Authorization" : vars.developerToken,
"Key" : "Value"
}]]]></http:headers>
</http:request>
<ee:transform doc:name="Transform Response">
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
result : "File has been successfully created"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>