在Volley请求中添加JWT令牌

时间:2015-12-11 18:26:24

标签: android node.js android-volley jwt express-jwt

我在后端使用Node.js,需要在GET请求的标头中发送一些params(参数)和一个jwt标记(用于授权)。 我在节点服务器上使用express-jwt模块。需要使用jwt令牌授权该请求。简单地将令牌作为params发送在那里不起作用。

    private void getInfo(final String instanceId, final String token) {
//token is jwt token
            StringRequest strReq = new StringRequest(Request.Method.GET,
                    Config.URL_GET_ID_INFO, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response);

                    try {
                        JSONObject responseObj = new JSONObject(response);
                        boolean error = responseObj.getBoolean("error");
                        String message = responseObj.getString("message");
                        if (!error) {

                            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + message,
                                    Toast.LENGTH_LONG).show();
                        }

                        // hiding the progress bar
                        //progressBar.setVisibility(View.GONE);

                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();

                       // progressBar.setVisibility(View.GONE);
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    //progressBar.setVisibility(View.GONE);
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String , String>();
                    params.put("instanceId", instanceId);

                    //Need to send jwt along with params, but do not know how to include it in the request.
                    Log.e(TAG, "Posting params: " + params.toString());

                    return params;
                }



            };

            // Adding request to request queue
            MyApplication.getInstance().addToRequestQueue(strReq);
        }

`

1 个答案:

答案 0 :(得分:0)

只需覆盖Request的方法getHeaders()

以下是一个例子:

@Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Authorization", "Bearer "+ yourToken);
            return params;
        }