0

i wanted to access my back-end using android phone. at this moment i want to do two things:

  1. login the mob app after being authenticated from the Back-end.
  2. Upload data to the back-end (back-end is PHP with Laravel Framework).

i first send email and password to the backend and gotten back a response of JWT token as shown:

"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4xMDI6ODAwMFwvYXBpXC92M1wvYXV0aGVudGljYXRlIiwiaWF0IjoxNDY2MDA5OTY3LCJleHAiOjE0NjYwMTM1NjcsImp0aSI6IjZjYjBlMTRjYTNkMjAzM2Q4OWM0NzM1M2ZjNjMzZTU2In0.GJGUjgy8U-uqjLSqJcysDTmgrNvxBHH03iBflLjsOwA"

Once the above token returned to the mob app, i want to send the same returned token for further post request, because the above jwt token is my key for accessing the back-end.

So my problem lies on sending the same token back to the back-end. it seems simple and stright forward since i have already started to communicate with my back end, and i have also checked my response by using post man.

loging in

and i can also got the user cridential using the jwt token on postman.

postman receive user cridentials as json

now the same token which works on postman is not working for my android. i know that my httpClient and httpPost are working since i have already send the email and password with it. i also know that my android post request is reaching the server since my returned result comes with an error message i built for an accepted token request, as shown below.

when android recives user cridentials as json

as you can see from the above snap shot. i first get the token inside a quotation (first highlighted), when posted for authentication. so i removed the quotation and posted the same token for getting user cridential but this time i got an error response which i built it on the Back-end.

so that is why i think my token is not going to the server properly. but i couldn't fix the problem. but i am guessing the token size is big, with a length of 490. so how shall i attach my token with the httpPost request? my code for the building the request is shown below:

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 have also tried using MultipartEntityBuilder for parsing the parameter (token in my case) but the MultipartEnityBuilder library was crasshing my program when building:

the MultipartEnityBuilder library is added to my project using the following dependencies:

//for accessing the MultipartEntity lbrary
compile "org.apache.httpcomponents:httpcore:4.2.4"
compile "org.apache.httpcomponents:httpmime:4.3"

the error because of the MultipartEntity

build error for the MultipartEntity library

So now my question is: how can i send the jwt token value from android to Laravel backend.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Lakie
  • 15
  • 2
  • 9
  • Token cannot be sent as form data for JWT. Refer [this] (https://github.com/tymondesigns/jwt-auth/wiki/Authentication). – linuxartisan Jun 16 '16 at 05:50

2 Answers2

0

Perhaps try using MultipartEntity instead and create a "part" for the token. I have adapted this closely related answer to your case:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
//here you create the token body    
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("token", jwtString, ContentType.TEXT_PLAIN);
HttpEntity reqEntity = builder.build();
httppost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

I hope this helps, please give it a try and let me know.

You can visit this blog on Multipart Upload with HttpClient and learn more.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • StringBody and MultipartEntity class does not exist and i couldn't import it either – Lakie Jun 15 '16 at 19:33
  • I have updated the answer and used `MultipartEntityBuilder` instead - see if you are able to import this one - also, please confirm your version of `HttpClient` library. – ishmaelMakitla Jun 15 '16 at 20:12
  • i have imported the MultipartEntityBuilder but crush when i run it. the library i used to import it is. 'compile' "'org.apache.httpcomponents:httpcore:4.2.4'" 'compile' "'org.apache.httpcomponents:httpmime:4.3'" ... the HttpClient libry is built in with IntelliJ IDEA ( org.apache.http.client.HttpClient ) – Lakie Jun 16 '16 at 14:22
  • Can you please update your question and paste the error log? I want to see how what may be causing the crash. – ishmaelMakitla Jun 16 '16 at 14:24
0

i have managed to solve my problem by simply setting the Authorization header with the token:

public String getJsonString2(String url, String jwtString) {

    String jsonString = "";

    // Creating HTTP client and post
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);

    httpPost.setHeader("Authorization", "Bearer \\{" +  jwtString + "\\}");

    // Making HTTP Request
    try {
        HttpResponse response = httpClient.execute(httpPost);
        jsonString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        System.out.println("Http String content: " + jsonString);
    } catch (ClientProtocolException e) {
        // writing exception to log
        e.printStackTrace();
    } catch (IOException e) {
        // writing exception to log
        e.printStackTrace();
    }
    return jsonString;
}
Lakie
  • 15
  • 2
  • 9