Contact Free trial Login

Box Connector Examples - Mule 4

Try out the following examples using Studio or XML.

Anypoint Studio Example: Create Folder, Retrieve Folder, and Delete Folder Flow

This example demonstrates the Create Folder, Retrieve Folder, and Delete Folder operations.

  1. Create a new Mule Project in Anypoint Studio.

  2. Add the following properties to the mule-artifact.properties file to hold your Box credentials and place it in the project’s src/main/resources directory.

    config.clientId=<Client ID>
    config.clientSecret=<Client Secret>
  3. Configure the HTTP Listener by adding a new HTTP Global Element. Click the plus sign next to the Connector configuration field.

  4. Configure the global element according to the table below:

    Parameter Value

    Name

    HTTP_Listener_config

    Protocol

    HTTP

    Host

    localhost

    Port

    8081

  5. 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 had been previously created.

    Path

    /boxdemo

  6. Now 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"
    }
  7. Drag the Create folder operation of the Box connector next to the DataWeave component to create a folder.

  8. Configure the Box connector by adding a new Box Global Element. Click the plus sign next to the Connector configuration field.

  9. 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. Leave as is.

    basePath

    /2.0

    The default value. Leave as is.

    protocol

    HTTPS

    The default value. Leave as is.

    localCallbackPath

    /callback

    The default value. Leave as is.

    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

    This URL needs to be hit 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 for 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" />
  10. Back in the properties editor of the 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]

  11. Drag a Logger component to log the newly created folder ID and configure the following properties.

    Parameter Value

    Display Name

    Logger to log the created Folder ID

    Generic

    Message

    Folder created with ID : #[payload.id]

  12. Drag the Get folder operation of the 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]

  13. Drag a Logger component to log the retrieved folder information and configure the following properties.

    Parameter Value

    Display Name

    Logger to log the retrieved folder info.

    Generic

    Message

    #[payload]

  14. Drag the Delete folder operation of the Box connector 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]

  15. Then, 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 got created, retrieved and deleted successfully"
    }
  16. Save and run the project as a Mule Application.

  17. Use the URL http://localhost:8081/authorize from a browser which you set for localAuthorizationUrl in global Box configuration element to initiate the OAUTH2 dance.

    This displays the following 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).

  18. Click Grant access to Box to grant the read and write permissions.

  19. Open a web browser and access the URL 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>
		<mule-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]" />
		<mule-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]" />
		<mule-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>

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.