2016-06-15 99 views
0

我想使用android手機訪問我的後端。此時我想做兩件事:如何發送/ POST JWT令牌從Android到Laravel後端

  1. 從後端驗證後登錄mob app。
  2. 將數據上傳到後端(後端是使用Laravel Framework的PHP)。

我第一發送電子郵件和密碼到後端,並得到回如圖JWT令牌的響應:

「eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4xMDI6ODAwMFwvYXBpXC92M1wvYXV0aGVudGljYXRlIiwiaWF0IjoxNDY2MDA5OTY3LCJleHAiOjE0NjYwMTM1NjcsImp0aSI6IjZjYjBlMTRjYTNkMjAzM2Q4OWM0NzM1M2ZjNjMzZTU2In0.GJGUjgy8U-uqjLSqJcysDTmgrNvxBHH03iBflLjsOwA」

一旦上述令牌返回到移動應用程序,我想發送相同的返回的令牌進一步發佈請求,因爲上面的jwt令牌是我訪問後端的密鑰。

所以我的問題在於發送相同的令牌回到後端。由於我已經開始與我的後端進行溝通,所以它看起來很簡單並且很直接,而且我也通過使用post man來檢查我的回覆。

loging in

,我也可以得到使用上郵差JWT令牌用戶cridential。

postman receive user cridentials as json

現在這對郵遞員的工作同樣不工作我的機器人。我知道我的httpClient和httpPost正在工作,因爲我已經發送了電子郵件和密碼。我也知道我的android post請求到達了服務器,因爲我的返回結果帶有一個我爲接受的令牌請求構建的錯誤消息,如下所示。

when android recives user cridentials as json

,你可以從上面的快照看到。我首先在引號(首先突出顯示)中獲取令牌,然後發佈進行身份驗證。所以我刪除了引號併發布了獲取用戶代碼的相同標記,但是這次我得到了一個我在後端構建的錯誤響應。

所以這就是爲什麼我認爲我的令牌沒有正確地進入服務器。但我無法解決問題。但我猜測令牌大小很大,長度爲490.那麼我怎麼把我的令牌附加到httpPost請求?我的用於建築物的請求代碼如下所示:

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; 

    } 

我也使用MultipartEntityBuilder用於解析參數(令牌在我的情況)嘗試,但在MultipartEnityBuilder文庫crasshing我的節目時建築物:

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

的錯誤,因爲的MultipartEntity

:MultipartEnityBuilder庫使用下面的依賴也會添加到我的項目0

build error for the MultipartEntity library

所以,現在我的問題是: 我怎樣才能發送從Android的jwt令牌值到Laravel後端。

+0

令牌不能作爲JWT表單數據發送。請參閱[本](https://github.com/tymondesigns/jwt-auth/wiki/Authentication)。 – linuxartisan

回答

0

我設法通過簡單地用標記設置Authorization頭解決我的問題:

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; 
} 
0

也許嘗試使用MultipartEntity,而不是創建令牌的「部分」。我已經適應this closely related answer到您的情況:

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(); 

我希望這可以幫助,請給它一個嘗試,讓我知道。

您可以訪問此blog on Multipart Upload with HttpClient並瞭解更多信息。

+0

StringBody和MultipartEntity類不存在,我也無法導入它 – Lakie

+0

我已經更新了答案並使用'MultipartEntityBuilder'來代替 - 請參閱是否能夠導入這一個 - 另外,請確認您的版本的'HttpClient'庫。 – ishmaelMakitla

+0

我已經導入了MultipartEntityBuilder,但是當我運行它時會暗戀。我用來導入它的庫是。 '編譯'''org.apache.httpcomponents:httpcore:4.2.4'' 'compile'「'org.apache.httpcomponents:httpmime:4.3'」... HttpClient libry內置IntelliJ IDEA(組織)。 apache.http.client.HttpClient) – Lakie