Search in sources :

Example 1 with AccessToken

use of com.twilio.sdk.auth.AccessToken in project api-snippets by TwilioDevEd.

the class TokenGenerator method main.

public static void main(String[] args) {
    // Required for all types of tokens
    String twilioAccountSid = "ACxxxxxxxxxxxx";
    String twilioApiKey = "SKxxxxxxxxxxxx";
    String twilioApiSecret = "xxxxxxxxxxxxxx";
    // Required for IP Messaging
    String ipmServiceSid = "ISxxxxxxxxxxxx";
    String deviceId = "someiosdevice";
    String identity = "user@example.com";
    String appName = "HipFlowSlackDockRC";
    String endpointId = appName + ":" + identity + ":" + deviceId;
    // Create IP messaging grant
    IpMessagingGrant grant = new IpMessagingGrant();
    grant.setEndpointId(endpointId);
    grant.setServiceSid(ipmServiceSid);
    // Create access token
    AccessToken token = new AccessToken.Builder(twilioAccountSid, twilioApiKey, twilioApiSecret).identity(identity).grant(grant).build();
    System.out.println(token.toJWT());
}
Also used : IpMessagingGrant(com.twilio.sdk.auth.IpMessagingGrant) AccessToken(com.twilio.sdk.auth.AccessToken)

Example 2 with AccessToken

use of com.twilio.sdk.auth.AccessToken in project api-snippets by TwilioDevEd.

the class Webapp method main.

public static void main(String[] args) {
    // Serve static files from src/main/resources/public
    staticFileLocation("/public");
    // Create a Faker instance to generate a random username for the connecting user
    final Faker faker = new Faker();
    // Create an access token using our Twilio credentials
    get("/token", "application/json", (request, response) -> {
        // Generate a random username for the connecting client
        final String identity = faker.firstName() + faker.lastName() + faker.zipCode();
        // Create Twilio Video messaging grant
        final VideoGrant grant = new VideoGrant();
        grant.configurationProfileSid = System.getenv("TWILIO_CONFIGURATION_SID");
        // Create access token
        final AccessToken token = new AccessToken.Builder(System.getenv("TWILIO_ACCOUNT_SID"), System.getenv("TWILIO_API_KEY"), System.getenv("TWILIO_API_SECRET")).identity(identity).grant(grant).build();
        // create JSON response payload 
        final HashMap<String, String> json = new HashMap<String, String>();
        json.put("identity", identity);
        json.put("token", token.toJWT());
        // Render JSON response
        final Gson gson = new Gson();
        return gson.toJson(json);
    });
}
Also used : VideoGrant(com.twilio.sdk.auth.VideoGrant) Faker(com.github.javafaker.Faker) HashMap(java.util.HashMap) AccessToken(com.twilio.sdk.auth.AccessToken) Gson(com.google.gson.Gson)

Aggregations

AccessToken (com.twilio.sdk.auth.AccessToken)2 Faker (com.github.javafaker.Faker)1 Gson (com.google.gson.Gson)1 IpMessagingGrant (com.twilio.sdk.auth.IpMessagingGrant)1 VideoGrant (com.twilio.sdk.auth.VideoGrant)1 HashMap (java.util.HashMap)1