mercredi 15 juin 2016

how to send/POST JWT token from Android to Laravel backend

i was trying to authenticate and post data from android to a Laravel backend.

i first send email and password to the backend and gotten back a response of JWT token as shown: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4xMDI6ODAwMFwvYXBpXC92M1wvYXV0aGVudGljYXRlIiwiaWF0IjoxNDY2MDA5OTY3LCJleHAiOjE0NjYwMTM1NjcsImp0aSI6IjZjYjBlMTRjYTNkMjAzM2Q4OWM0NzM1M2ZjNjMzZTU2In0.GJGUjgy8U-uqjLSqJcysDTmgrNvxBHH03iBflLjsOwA"

the above token only authenticates the user but do not contain the user credential so, i used the same HttpClient to post the token and get json form of user cridential. but this time the token is not being accepted on the server side. i get an error of missing token.

public String getJsonString2(String url, String jwtString) {

    String jsonString = "";

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    List nameValuePair = new ArrayList(1);
    nameValuePair.add(new BasicNameValuePair("token", jwtString));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    }
    catch (UnsupportedEncodingException e) {
        // writing error to Log
        e.printStackTrace();
    }

    // Making HTTP Request
    try {
        HttpResponse response = httpClient.execute(httpPost);
        jsonString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

    } catch (ClientProtocolException e) {
        // writing exception to log
        e.printStackTrace();

    } catch (IOException e) {
        // writing exception to log
        e.printStackTrace();
    }

    return jsonString;

}

i used the same code for posting the email and password but it is not working for the token.

i have also test my web app using google postman application and it responds with the proper user cridential.

So now my questions are: 1. how can i send the jwt token value in android. 2. is the following value too much to be used in httpPost? httpPost.getEntity().getContentLength() = 278 3. could it be my backend problem?, but keep in mind that i have test the service using postman. 4. and finally am i doing a proper way of communication with my backend?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire