Skip to contentSkip to navigationSkip to topbar
On this page

Twilio Connect Java Quickstart



Overview

overview page anchor

Twilio Connect allows developers to obtain authorization to make calls, send text messages, purchase phone numbers, read access logs and perform other API functions on behalf of another Twilio account holder.

As an example, imagine you want to access the Twilio account of a user of your web application to provide in-depth analytics of their Twilio account activity. In this quickstart we'll solve this problem by creating your first Twilio Connect App, placing the "Connect" button on your website so users can authorize your app to access their Twilio account data and make API requests against their account.


Creating Your First Twilio Connect App

creating-your-first-twilio-connect-app page anchor

Let's jump right in and create our first Connect app. Log in to your Twilio account dashboard, select "Apps" and click the "Create Connect App" button. Fill in the top section with the name of your application and your company information.

Next, assign an Authorize URL to your Connect application. The Authorize URL is the URL that Twilio will redirect the user's browser to after they have authorized your application to access their Twilio account. Later on in the quickstart we'll demonstrate how the Authorize URL is used.

Lastly, select the access rights your Connect app requires on the user's account. For this example we will access call logs for analytics, so we'll choose "Read all account data".

Here's what our sample Connect application looks like:

Rossum Twilio Connect App.

Click 'Save Changes' and you're done!


Placing the Connect Button on Your Website

placing-the-connect-button-on-your-website page anchor
Twilio Connect.

The Connect button is where your customers will start the process of authorizing your Connect App to access their Twilio account. You can generate the code needed to place this button on your website with the Twilio Connect button HTML generator.

After saving your application you will see a popup with HTML code. Copy the generated code and paste it into the HTML of your website where you would like the button to appear. If you ever need to generate this HTML again, click on the "Generate Connect Button HTML" link at the bottom of your Connect App Details page.


Testing the Authorization Workflow

testing-the-authorization-workflow page anchor

With the Twilio Connect button now on your website, browse to the page where you placed the HTML and click the Connect button. Verify that the information displayed on the authorization screen is correct.

After completing the app authorization process, you are redirected to the Authorize URL you specified when creating your Connect App. Appended to that URL is an Account Sid URL parameter with a value that looks like this:

1
http://www.example.com/twilio/authorize?AccountSid=AC12345
2
Your Connect App's Authorize URL Customer's SID
3

Your application should extract the AccountSid value from the URL and associate it with the user's account within your application. After extracting the AccountSid, we recommend that you redirect the user to another page within your app so the AccountSid isn't hanging around. Let's show an example using Java.

(warning)

Warning

This tutorial assumes you have a Java development environment with a Web server capable of running Java servlets and the twilio-java helper library. Please see our post on setting up your environment if you need help installing those programs.

1
package com.twilio;
2
import javax.servlet.http.HttpServlet;
3
import javax.servlet.http.HttpServletRequest;
4
import javax.servlet.http.HttpServletResponse;
5
import java.io.IOException;
6
7
import com.twilio.sdk.verbs.TwiMLResponse;
8
import com.twilio.sdk.verbs.TwiMLException;
9
import com.twilio.sdk.verbs.Sms;
10
11
public class TwilioServlet extends HttpServlet {
12
13
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
14
// Retrieve the Connect user's Account Sid
15
String accountSid = request.getParameter("AccountSid");
16
17
// Store this account sid in your database, so you can retrieve it
18
// later. You will need to write this section of the code.
19
20
// Finally, redirect the user to your app after you've gathered their
21
// SID
22
response.sendRedirect("http://example.com/myapp");
23
}
24
}

Making an Authorized Request

making-an-authorized-request page anchor

With the user's Account Sid in hand, you can now request data from their account via the Twilio REST API. A request to retrieve data from a user's account is nearly identical to a request made against your account, with one key difference. Instead of authenticating with your own AccountSid, you authenticate with the Account Sid retrieved during the authorization process and your account's Auth Token.

Here is a request to retrieve call logs from an account using the Java helper library. Pay special attention to line 4 where the customer's Account Sid is specified instead of your own:

1
import java.util.Map;
2
import java.util.HashMap;
3
4
import com.twilio.sdk.TwilioRestClient;
5
import com.twilio.sdk.TwilioRestException;
6
import com.twilio.sdk.resource.instance.Account;
7
import com.twilio.sdk.resource.instance.Call;
8
import com.twilio.sdk.resource.list.CallList;
9
10
public class CallRetriever {
11
12
// The customer's Account Sid
13
public static final String ACCOUNT_SID = "AC123";
14
15
// Your own Auth Token
16
public static final String AUTH_TOKEN = "456bef";
17
18
public static void main(String[] args) throws TwilioRestException {
19
20
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
21
Account mainAccount = client.getAccount();
22
CallList calls = mainAccount.getCalls();
23
for (Call call : calls) {
24
System.out.println("From: " + call.getFrom() + " To: " + call.getTo());
25
}
26
}
27
}
28

You're Done! Now What?

youre-done-now-what page anchor

Retrieving call logs on behalf of your customers is just the start of what you can accomplish with Twilio Connect. Visit the complete Connect documentation and best practices to learn more about how to integrate Connect's additional capabilities to your applications.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.