我正在尝试使用身份验证标头发送请求,但似乎服务器无法识别客户端。 我使用了this教程,并按如下方式实现了拦截器:
public class AuthenticationInterceptor implements Interceptor {
private String authId;
private String authToken;
public AuthenticationInterceptor(String authId, String authToken) {
this.authId = authId;
this.authToken = authToken;
}
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder();
if (authId != null && authToken != null) {
Timber.d("adding auth headers for: " + this);
builder.header("auth_id", authId)
.header("auth_token", authToken);
}
Request request = builder.build();
return chain.proceed(request);
}
}
当我尝试向服务器发送经过身份验证的请求时,它会返回错误响应409.服务器人员告诉我,我错过了那些参数:(例如Postman收到的)
“accept”: [
“*/*”
],
“accept-encoding”: [
“gzip, deflate”
],
“cookie”: [
“PHPSESSID=ah1i1856bkdln5pgmsgjsjtar3"
]
我认为使用Dagger2可能会导致此问题(请参阅here),所以我已经隔离了okHttpClient,但它仍然不起作用。
这是我的用法实现(非常简单):
Retrofit retrofit;
OkHttpClient client;
AuthenticationInterceptor authenticationInterceptor;
HttpLoggingInterceptor loggingInterceptor;
private void testHeaders() {
loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Timber.i(message);
}
});
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.baseUrl(BuildConfig.SERVER_ADDRESS)
.build();
retrofit.create(EntrOnline.class).getLoginToken("email@email.com", "XXX").enqueue(new Callback<NewAccount>() {
@Override
public void onResponse(Call<NewAccount> call, Response<NewAccount> response) {
authenticationInterceptor = new AuthenticationInterceptor(response.body().getAuthId(), response.body().getAuthToken());
client = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(authenticationInterceptor)
.build();
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.baseUrl(BuildConfig.SERVER_ADDRESS)
.build();
retrofit.create(EntrOnline.class).getKeys("50022d8a-b309-11e7-a902-0ac451eb0490").enqueue(new Callback<List<NewEkey>>() {
@Override
public void onResponse(Call<List<NewEkey>> call, Response<List<NewEkey>> response) {
Timber.d("Test Api");
}
@Override
public void onFailure(Call<List<NewEkey>> call, Throwable t) {
}
});
}
@Override
public void onFailure(Call<NewAccount> call, Throwable t) {
}
});
}
谢谢!
答案 0 :(得分:0)
@Headers("Accept: application/json")
@FormUrlEncoded
@POST("sendWalletMoney")
Call<WalletResponse> sendWalletMoney(@Field("sender") String sender,
@Field("receiver") String receiver,
@Field("amount") String amount,
@Field("trnsx_type") String trnsx_type,
@Field("currency") String currency,
@Field("module_name") String module_name);
答案 1 :(得分:0)
看起来像server issue。 您可以尝试添加他在拦截器中谈到的标题。
builder.addHeader("Accept", "*/*").addHeader("accept-encoding", "gzip, deflate")
但是应该在网上使用cookie来跟踪当前会话。不应该在API上使用。