Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i generate JWT token from android

Tags:

android

token

jwt

How do i generate JWT token from android. I tried the following :

 token = JWT.create().withClaim("email",username)
                        .sign(Algorithm.HMAC256("secret"));
                System.out.println(" JWS token : "+ token);

But i got this exception :

java.lang.NoSuchMethodError: No static method encodeBase64URLSafeString([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/ext.jar)
                                                          at com.auth0.jwt.JWTCreator.sign(JWTCreator.java:283)
                                                          at com.auth0.jwt.JWTCreator.access$100(JWTCreator.java:23)
                                                          at com.auth0.jwt.JWTCreator$Builder.sign(JWTCreator.java:264)
                                                          at se.stigasoft.netwrapper.NetCom.jwtWork(NetCom.java:321)

I tried other methos from some other library too.

 String compactJws = Jwts.builder()
            .setSubject("Joe")
            .signWith(SignatureAlgorithm.HS256, "secret".getBytes())
            .compact();

this one generates the token. But i dont know how to send my data like name,value pair that i used to send in the post method .

. Please help

like image 311
beginner Avatar asked Jan 24 '17 04:01

beginner


People also ask

Does JWT work on mobile?

On login, if the JWT token is valid then it allows the mobile app user into the app. On logout, it removes the token on the user's device.


1 Answers

To generate JWT token there is the way to generate:

1) add dependency in gradle

implementation 'io.jsonwebtoken:jjwt:0.7.0'

2) add the following code on the basis of the parameters.

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             String jwt = Jwts.builder().claim("emailId","[email protected]").claim("emailIdOTP", "123456")
                .claim("phoneNo", "1111111111")
                .signWith(SignatureAlgorithm.HS256, "secret".getBytes())
                .compact();
                Log.v("JWT : - ",jwt);
            }
        });
like image 93
Gyan Swaroop Awasthi Avatar answered Sep 18 '22 06:09

Gyan Swaroop Awasthi