0

I am trying to generate access token for twilio chat but got this error:I have been trying to figure out where the error is coming from but can't get it figured out. I will really appreciate your help. Thanks

E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.zihron.projectmanagementapp, PID: 16355
                      java.lang.Error: javax.xml.datatype.DatatypeConfigurationException: Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl not found
                          at javax.xml.bind.DatatypeConverterImpl.<clinit>(DatatypeConverterImpl.java:744)
                          at javax.xml.bind.DatatypeConverter.<clinit>(DatatypeConverter.java:78)
                          at javax.xml.bind.DatatypeConverter.printBase64Binary(DatatypeConverter.java:547)
                          at io.jsonwebtoken.impl.Base64Codec.encode(Base64Codec.java:24)
                          at io.jsonwebtoken.impl.Base64UrlCodec.encode(Base64UrlCodec.java:22)
                          at 
 io.jsonwebtoken.impl.AbstractTextCodec.encode(AbstractTextCodec.java:31)
                          at io.jsonwebtoken.impl.DefaultJwtBuilder.base64UrlEncode(DefaultJwtBuilder.java:314)
                          at io.jsonwebtoken.impl.DefaultJwtBuilder.compact(DefaultJwtBuilder.java:282)
                          at com.twilio.jwt.Jwt.toJwt(Jwt.java:100)
                          at ZihronChatApp.token.TokenGenerator.getToken(TokenGenerator.java:34)
                          at com.zihron.projectmanagementapp.ChatActivity.onCreateView(ChatActivity.java:43)

I have my details below:

public  AccessToken getToken() {
        // Required for all types of tokens
        String twilioAccountSid ="AC601f2c7***7ed***640***264c***d0d";
        String twilioApiKey = "SK684***dda***c81****6c4a****093**";
        String twilioApiSecret ="96****dbc06****b74d50***b9***3*4";
        String serviceSid="IS***a29****e24****5d****4b20**3e*";

        String identity = "joshua.hamilton@gmail.com";

        ChatGrant grant = new ChatGrant();
        grant.setServiceSid(serviceSid);

        AccessToken token = new AccessToken.Builder(twilioAccountSid, 
       twilioApiKey, twilioApiSecret)
                .identity(identity).grant(grant).build();

        Log.e("++==--",""+token.toJwt());

        //.identity(identity).grant(grant);

     return token;
    }

1 Answers1

0

Twilio developer evangelist here.

The Twilio Java library is not intended for use within Android projects.

The issue here is that you should not be storing your credentials within your application. A malicious user could decompile your application, take your credentials and abuse them.

Instead, you should create a server (or use some sort of serverless environment, like Twilio Functions) that can run this code and return the token. You should then make an HTTP request from your Android application to fetch that token. Check out the Twilio Programmable Chat Android Quickstart to see how it's done there.

philnash
  • 70,667
  • 10
  • 60
  • 88