Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit 13635bb

Browse files
author
Jozsef Vass
committed
video token access server
1 parent db40231 commit 13635bb

File tree

7 files changed

+17
-374
lines changed

7 files changed

+17
-374
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export TWILIO_ACCOUNT_SID=
22
export TWILIO_API_KEY=
33
export TWILIO_API_SECRET=
4-
export TWILIO_CONFIGURATION_SID=

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# Video Quickstart for Java
1+
# Video Access Token Server for Java
22

3-
This application should give you a ready-made starting point for writing your
4-
own video apps with Twilio Video. Before we begin, we need to collect
3+
This server-side application demonstrates generating Access Token for Twilio Video.
4+
Before we begin, we need to collect
55
all the config values we need to run the application:
66

77
| Config Value | Description |
88
| :------------- |:------------- |
9-
Configuration Profile SID | Identifier for a set of config properties for your video application - [find yours here](https://www.twilio.com/console/video/profiles).
109
Account SID | Your primary Twilio account identifier - find this [in the console here](https://www.twilio.com/console).
1110
API Key | Used to authenticate - [generate one here](https://www.twilio.com/console/video/dev-tools/api-keys).
1211
API Secret | Used to authenticate - [just like the above, you'll get one here](https://www.twilio.com/console/video/dev-tools/api-keys).
@@ -17,7 +16,7 @@ When you generate an API key pair at the URLs above, your API Secret will only
1716
be shown once - make sure to save this in a secure location,
1817
or possibly your `~/.bash_profile`.
1918

20-
## Setting Up The Java Application
19+
## Setting up the Java Application
2120

2221
This application uses the lightweight [Spark Framework](http://sparkjava.com/), and
2322
requires Java 8 and [Maven](https://maven.apache.org/install.html).
@@ -28,7 +27,7 @@ Begin by creating a configuration file for your application:
2827
cp .env.example .env
2928
```
3029

31-
Edit `.env` with the four configuration parameters we gathered from above.
30+
Edit `.env` with the three configuration parameters we gathered from above.
3231

3332
Next, we compile our application code:
3433

@@ -42,10 +41,7 @@ Now we should be all set! Run the application using the `java -jar` command.
4241
java -jar target/video-quickstart-1.0-SNAPSHOT.jar
4342
```
4443

45-
Your application should now be running at [http://localhost:4567](http://localhost:4567).
46-
Select any room name and join the room. Join the same room with another user in another browser tab or window to start video chatting!
47-
48-
![screenshot of chat app](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/video2.original.png)
44+
To generate Access Token, visit [http://localhost:4567?identity=alice&room=example](http://localhost:4567?identity=alice&room=example).
4945

5046
## License
5147

pom.xml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,10 @@
2424
</exclusion>
2525
</exclusions>
2626
</dependency>
27-
<dependency>
28-
<groupId>com.google.code.gson</groupId>
29-
<artifactId>gson</artifactId>
30-
<version>2.2.4</version>
31-
</dependency>
32-
<dependency>
33-
<groupId>com.github.javafaker</groupId>
34-
<artifactId>javafaker</artifactId>
35-
<version>0.2</version>
36-
</dependency>
3727
<dependency>
3828
<groupId>com.twilio.sdk</groupId>
3929
<artifactId>twilio</artifactId>
40-
<version>(7.0,7.9)</version>
30+
<version>7.9.0</version>
4131
</dependency>
4232
<dependency>
4333
<groupId>ch.qos.logback</groupId>

src/main/java/com/twilio/Webapp.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,53 @@
11
package com.twilio;
22

3-
import com.github.javafaker.Faker;
4-
import com.google.gson.Gson;
53
import com.twilio.jwt.accesstoken.AccessToken;
64
import com.twilio.jwt.accesstoken.VideoGrant;
75

86
import java.io.File;
97
import java.io.FileInputStream;
10-
import java.util.HashMap;
118
import java.util.Properties;
129

1310
import static spark.Spark.afterAfter;
1411
import static spark.Spark.get;
15-
import static spark.Spark.staticFileLocation;
1612

1713
public class Webapp {
1814

1915
public static void main(String[] args) throws Exception {
2016
// Load the .env file into environment
2117
dotenv();
2218

23-
// Serve static files from src/main/resources/public
24-
staticFileLocation("/public");
25-
26-
// Create a Faker instance to generate a random username for the connecting user
27-
Faker faker = new Faker();
28-
2919
// Log all requests and responses
3020
afterAfter(new LoggingFilter());
3121

3222
// Create an access token using our Twilio credentials
33-
get("/token", "application/json", (request, response) -> {
23+
get("/", (request, response) -> {
3424
// Generate a random username for the connecting client
35-
String identity = faker.firstName() + faker.lastName() + faker.zipCode();
25+
final String identity = request.queryParams("identity") != null ? request.queryParams("identity") : "identity";
26+
27+
System.out.println(identity);
3628

3729
// Create Video grant
38-
VideoGrant grant = new VideoGrant();
39-
grant.configurationProfileSid = System.getProperty("TWILIO_CONFIGURATION_SID");
30+
final VideoGrant grant = new VideoGrant();
31+
grant.setRoom(request.queryParams("room"));
4032

4133
// Create access token
42-
AccessToken token = new AccessToken.Builder(
34+
final AccessToken token = new AccessToken.Builder(
4335
System.getProperty("TWILIO_ACCOUNT_SID"),
4436
System.getProperty("TWILIO_API_KEY"),
4537
System.getProperty("TWILIO_API_SECRET")
4638
).identity(identity).grant(grant).build();
4739

48-
// create JSON response payload
49-
HashMap<String, String> json = new HashMap<>();
50-
json.put("identity", identity);
51-
json.put("token", token.toJwt());
52-
53-
// Render JSON response
54-
Gson gson = new Gson();
55-
return gson.toJson(json);
40+
return token.toJwt();
5641
});
57-
58-
5942
}
6043

6144
private static void dotenv() throws Exception {
62-
File env = new File(".env");
45+
final File env = new File(".env");
6346
if (!env.exists()) {
6447
return;
6548
}
6649

67-
Properties props = new Properties();
50+
final Properties props = new Properties();
6851
props.load(new FileInputStream(env));
6952
props.putAll(System.getenv());
7053
props.entrySet().forEach(p -> System.setProperty(p.getKey().toString(), p.getValue().toString()));

src/main/resources/public/index.html

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/resources/public/quickstart.js

Lines changed: 0 additions & 153 deletions
This file was deleted.

0 commit comments

Comments
 (0)