A connector allows you to invoke a third-party product's functionality from the WSO2 Enterprise Service Bus (ESB) message flow, enabling you to connect to and interact with the APIs of services such as Twitter, Salesforce, and JIRA. For example, if you enable the Twitter connector in your ESB instance, your message flow can call the Twitter APIs. You can receive requests containing a user's Twitter name and password, log into the user's Twitter account, get a list of his/her followers, and write that information to a Google spreadsheet. You can also use one or more connectors to manage complex business use cases.
Before you begin,
- Go to https://twitter.com/ and create an account if you do not have one already.
- Download the WSO2 Twitter ESB connector from here. You can also go to WSO2 Store at https://store.wso2.com, search for the word "Twitter", and download the Twitter connector that appears under ESB Connectors.
- Install cURL on your machine if you do not already have it installed (you will use this to test the application by sending a sample request).
Let's get started!
Creating a Twitter application
To use Twitter APIs, you first create a Twitter application through which you authenticate yourself and communicate with Twitter.
Go to https://apps.twitter.com/ and click Create New App.
Enter the following information in the form that opens, scroll down, and click Create your Twitter application.
Name TwitterESBConnect (or any other name of your choice) Description Verifies credentials and retrieves user's details. Website http://127.0.0.1
- Click the application to open it, go to its Permissions tab, select the Read, Write and Access direct messages option, and click Update Settings.
- Go to the application's Keys and Access Tokens tab and generate a consumer key and secret pair and an access token by clicking the respective buttons. These tokens are required to authenticate any application/API that tries to invoke the Twitter APIs.
- Copy the following values, as you will need them later in this tutorial.
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
Developing a REST API to connect to the Twitter application
Let's use the WSO2 ESB Tooling environment to develop our REST API. WSO2 ESB Tooling provides user-friendly, configuration-driven development capabilities. Based on the Eclipse development environment, its simple graphical editor is ideal for developing and managing services, features, and artifacts.
Click the Tooling link on https://wso2.com/products/enterprise-service-bus/.
From the dialog box that opens, click the Download button, and select a tooling build that suits your OS. We recommend that you use the complete plug-in installation with pre-packaged Eclipse, so that you do not have to install Eclipse separately.
Tip: If you cannot figure out which ESB Tooling package to download, click here.
Extract the tooling package and run Eclipse either by double-clicking the Eclipse icon or navigating to the extracted folder using the Command-line/Terminal and running the
Tip: If you get any errors when you try to start Eclipse, such as the file being damaged or unable to be opened, see this.
The Eclipse user interface opens. Click the Developer Studio -> Open Dashboard menu at the top, and then click the ESB Solution Project link.
Tip: An ESB Solution Project is a combination of several project types. It allows you to save different artifact types under one project. For example, if your project requires an ESBConfig, registry resource, connector exporter, and a composite application, create an ESB Solution Project instead of creating each type of project separately.
For more information about WSO2 ESB project types, see Working with ESB Artifacts.
In the dialog box that opens, give a name for the project (in this tutorial, let's call it TwitterConnectPrj), and click Finish.
Next, let's add the Twitter connector to the project that you just created.
- Back in the Eclipse UI, right-click the main configuration project (i.e., TwitterConnectPrj), and click Add or Remove Connector.
- In the dialog box that opens, click the Add connector option, click Next, give the location of the Twitter connector that you downloaded in the Before you begin section at the beginning of this tutorial, and click Finish.
You have now added the connector to your project. Next, let's create the REST API.
- Right-click the project that you just created and click New-> REST API from the popup menu.
- Select the Create a New API Artifact option from the dialog box that opens, click Next, enter the following details, and click Finish.
- Name: TwitterConnectAPI
- Context: /twitterconnect
After creating the API, the Twitter connector operations will be available in the tools palette. You can drag and drop connector operations into your API in the Design view. Let's do that next.
In the Project Explorer, click the API you just created, go to the Design tab, and drag and drop the
initoperation from the Twitter Connector folder in the tools palette to the canvas on the right.
Tip: You can go to the Source tab to see the code being auto-generated every time you edit something in the Design tab. You can directly type in code on the Source tab, too.
- Click the
initoperation, go to its Properties tab, and give the Consumer Key, Consumer Secret, Access Token, and Access Token Secret values that you copied in step 5 of the previous section.
- Drag and drop the
getAccountVerifyCredentialsoperation from the tools palette onto the canvas to the right of the
getAccountVerifyCredentialsoperation uses Twitter's Verify Credentials API and returns the user's details after credential verification.
Expand the Mediators folder in the tools palette and drag and drop the
Respondmediator to the canvas on the right.
Tip: The Respond mediator stops processing the current message and sends the message back to the client as a response. For more information on the Respond mediator, click here.
- Save all your changes.
You have now completed creating your REST API. Next, let's create an archive file of the tooling project that you just created.
Exporting the project to a composite application archive
- In the Project Explorer, right-click the connector project (its name ends with "ConnectorExporter") and click New -> Add/Remove Connectors.
- Select the Add connector option in the dialog box that opens, click Next to go to the next screen, select from the file system the Twitter connector that you downloaded in the Before you begin section at the beginning of this tutorial, and click Finish.
- In the Project Explorer, click the pom.xml file of the composite application project, go to its Dependencies tab, and note that you have two project artifacts available: the configuration project and the connector project.
- Right-click the C-App project and select Export Composite Application Project from the pop-up menu.
- Give a name and location for the composite application archive file and click Next.
- Select both artifacts to be included in the composite application archive and click Finish.
You have now created a composite archive of the tooling project. Let's import that to WSO2 Integration Cloud.
Creating an ESB application in WSO2 Integration Cloud using the archive
- Log in to WSO2 Integration Cloud by going to https://wso2.com/cloud/.
- Sign in to WSO2 Cloud and go to the Integration Cloud tab. Then, click ESB Compsite Application.
Select the Local File System option and click Continue.
Enter the following information in the form that opens, scroll down, and click Create.
- Application Name: TwitterESBConnect (or any other name of your choice).
- Upload the archive file you created before.
Note that the created application opens in the Integration Cloud. Copy its endpoint URL that appears under the Endpoints section in the Overview tab.
Next, let's invoke the project.
Invoking the application created in the Integration Cloud
REST endpoints are presented in WSO2 Integration Cloud as Swagger definitions. Swagger definitions can come in the JSON language or YAML. In the Integration Cloud, the default is JSON. You can use a tool like the Swagger Editor to get the HTTP endpoint of a REST API in Swagger.
- Access the URL you copied in the previous step using a Web browser and copy the Swagger definition that opens.
- Go to http://editor.swagger.io/, paste the Swagger definition in the left-hand panel, and click the Try this operation link of the GET operation on the right-hand panel.
- Copy the endpoint URL of the GET request.
Go to the Command-line or Terminal and execute the following command:
- Note that Twitter's Verify Credentials API is invoked and you get the Twitter account owner's information (e.g., name, screen name, location etc.) on the terminal.
You have now used the Twitter connector from WSO2 Store to invoke Twitter's Verify Credentials API using an application deployed in WSO2 Integration Cloud.