简体   繁体   English

在 Android Java 中使用用户名和密码传递时,如何使用不记名令牌验证 api?

[英]How to authenticate api using bearer token when passed with username and password in Android Java?

I am using the Retrofit library to make a post request to a server through API, sending username and password and getting in response a session token.我正在使用 Retrofit 库通过 API 向服务器发出发布请求,发送用户名和密码并获得 session 令牌作为响应。 I am using this token (in the header as bearer token) to make a POST to the server with latitude and longitude to get the data but the method is ending up repeatedly in onFailure method of enqueue().我正在使用这个令牌(在 header 作为不记名令牌)向服务器发出经纬度的 POST 以获取数据,但该方法在 enqueue() 的 onFailure 方法中反复结束。

@POST("api/auth/login/username/{username}/password/{password}")
Call<ResponseBody> login(@Path("username")String username, @Path("password")String password);

@POST("api/products/get")
Call<ResponseBody> getData(@Header("Authorization") String authorization);Call<ResponseBody> 

In MainActivity:在 MainActivity 中:

Retrofit.Builder builder=new Retrofit.Builder()
                           .baseUrl("fake url")
                           .addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit=builder.build();
DataApi companyApi=retrofit.create(DataApi.class);
private String Token="";tokenCall=comapnyApi.login("data","!fakedata");
        tokenCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()){
                    try {
                        Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    Toast.makeText(MainActivity.this, "Wrong Username and password", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Couldn't connect to Fake company Server Api", Toast.LENGTH_SHORT).show();
            }
        });
    Call<ResponseBody> Data=companyApi.getData(Token);
    Data.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()){
                try {
                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                Toast.makeText(MainActivity.this, "Api Token Expired", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Couldn't connect to Fake Company Server", Toast.LENGTH_SHORT).show();
        }
    });

I think it is because you make the second request just after you did the first one.我认为这是因为您在完成第一个请求之后提出了第二个请求。 enqueue is async so when you do you first request, I mean tokenCall.enqueue(), its response come later so the second api call or Data.enqueue() is invoked with token == "". enqueue 是异步的,所以当你第一次请求时,我的意思是 tokenCall.enqueue(),它的响应稍后出现,所以第二个 api 调用或 Data.enqueue() 使用 token == "" 调用。 So make the second api call in onResponse of the first api call.因此,在第一个 api 调用的 onResponse 中进行第二个 api 调用。

private String Token="";
tokenCall=comapnyApi.login("data","!fakedata");
    tokenCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()){
                try {
                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                    Token = response.body().string();
                    Call<ResponseBody> Data=companyApi.getData(Token);
                    Data.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if (response.isSuccessful()){
                                try {
                                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            else {
                                Toast.makeText(MainActivity.this, "Api Token Expired", Toast.LENGTH_SHORT).show();
                            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Couldn't connect to Fake Company Server", Toast.LENGTH_SHORT).show();
        }
    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                Toast.makeText(MainActivity.this, "Wrong Username and password", Toast.LENGTH_SHORT).show();
            }
        }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Couldn't connect to Fake company Server Api", Toast.LENGTH_SHORT).show();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中使用用户名和密码在sharepoint 2010中进行身份验证? - How to authenticate in sharepoint 2010 using username & password in Java? 在 Android 中使用 Firestore 仅使用用户名和密码进行身份验证 - Authenticate using only username and password with firestore in Android 使用 API 后访问 java 中的不记名令牌 - Accessing bearer token in java using post API 使用不记名令牌在 java 中调用 GET API - Calling GET API in java using bearer token 如何在 java 中调用需要不记名令牌的 api? - How to call an api that needs a bearer token in java? java中使用用户名、密码、令牌的URLConnection - URLConnection in java using username, password, token 使用HttpClient验证用户名和密码 - Using HttpClient to authenticate username and password 如何使用sshj java api连接用户名和密码的远程机器? - How to connect to a remote machine with username and password using sshj java api? 如何使用 Java 正则表达式验证 Android 上的用户名和密码? - How to validate username and password on Android using Java regex? 无法使用Java在MongoDB中使用密码对用户名进行身份验证 - Can't authenticate username with password in MongoDB with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM