【问题标题】:How to print private API-endpoint JSON-objects - protected by Auth0-JWT in Android?如何在 Android 中打印受 Auth0-JWT 保护的私有 API 端点 JSON 对象?
【发布时间】:2021-04-14 11:46:42
【问题描述】:

我有一个带有 NodejS 后端的 Android 应用程序。后端提供了一个私有 API 端点,我已使用 Auth0 对其进行保护。

这是我的 NodeJS 代码:

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

要从 Android 应用程序调用我的私有 API,我使用了 code-samples from Auth0

要从端点打印我的消息,我想使用此功能:

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

我在这里调用这个函数:

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

当我使用我的 Auth0 帐户登录并调用 API 时,我会收到 Toast 消息:API 调用成功,当我调用 print_private_api_message() 时,我的 NodeJS 控制台已登录 UnauthorizedError:未找到授权令牌。

所以,我的问题是:如何使用 accessToken 并从受保护的 API 端点打印消息?

【问题讨论】:

    标签: java node.js android-studio jwt auth0


    【解决方案1】:

    您需要发送授权标头。您可以在以下问题中看到如何:How to send Authorization header in Android using Volley library?

    你需要添加的部分是生成头:

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

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2016-01-18
      • 2012-05-06
      • 2015-08-11
      • 2017-11-28
      相关资源
      最近更新 更多