[英]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.