Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - How to print private API-endpoint JSON-objects - protected by Auth0-JWT in Android?

I have an Android application with a NodejS-Backend. The backend provides an private API-endpoint, which I have protected with Auth0.

This is my NodeJS-Code:

app.get('/api/private', jwtCheck, function(req, res) {
  res.json({
    message: 'Hello World from private API-Endpoint!'
  });
});

To call my private API from the Android application I used the code-samples from Auth0.

To print my message from the endpoint I wanna use this function:

    private void print_private_api_message() {
    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    String url = "http://10.0.2.2:3000/api/private";

    JsonArrayRequest request = new JsonArrayRequest(com.android.volley.Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONArray>() {

        public void onResponse(JSONArray response) {
            try {
                JSONObject message_private = response.getJSONObject(0);
                String message_from_backend = message_private.getString("message");

                Toast.makeText(MainActivity.this, message_from_backend, Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new com.android.volley.Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    queue.add(request);
}

I call this function here:

            public void onResponse(@NotNull Call call, @NotNull final Response response) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (response.isSuccessful()) {
                        Toast.makeText(MainActivity.this, "API call success!", Toast.LENGTH_SHORT).show();
                        print_private_api_message();
                    } else {
                        Toast.makeText(MainActivity.this, "API call failed.", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

When I'm logged in with my Auth0-account and call the API I get the Toast-Message: API call success and when I call print_private_api_message() my NodeJS-console logged UnauthorizedError: No authorization token was found.

So, my question is: How can I use the accessToken and print the message from my protected API-endpoint?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to send the authorization header. You can see how in the following question: How to send Authorization header in Android using Volley library?

The part are you need to add is the generation header:

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headerMap = new HashMap<String, String>();
                headerMap.put("Content-Type", "application/json");
                headerMap.put("Authorization", "Bearer " + ACCESS_TOKEN);
                return headerMap;
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...