인증 헤더가있는 요청을 보내려고하지만 서버가 클라이언트를 식별 할 수없는 것으로 보입니다. 다음과 같이 나는 this 자습서를 사용하고, 인터셉터를 구현 :Retrofit 2를 사용하여 요청할 헤더 추가
내가 서버에 인증 요청을 보내려고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)는 서버 사람이 내가 사람들을 누락 말해 줬어 반환 PARAMS : (예를 들어 우편 배달부 수신)
“accept”: [
“*/*”
],
“accept-encoding”: [
“gzip, deflate”
],
“cookie”: [
“PHPSESSID=ah1i1856bkdln5pgmsgjsjtar3"
]
나는 (here 참조) Dagger2이 문제가 발생할 수 있습니다 사용하여 생각, 그래서 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 protected]", "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) { } });
}
감사 :
여기 내 사용의 구현 (매우 간단)입니다!
사용자 지정 헤더를 추가했습니다. 도움이되지 않았습니다. –