2017-10-17 2 views
2

인증 헤더가있는 요청을 보내려고하지만 서버가 클라이언트를 식별 할 수없는 것으로 보입니다. 다음과 같이 나는 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) { 
    
        } 
    }); 
    

    }

감사 :

  • 여기 내 사용의 구현 (매우 간단)입니다!

  • 답변

    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); 
    
    +0

    사용자 지정 헤더를 추가했습니다. 도움이되지 않았습니다. –

    0

    server issue처럼 보입니다. 인터셉터에서 언급 한 헤더를 추가 할 수 있습니다.

    builder.addHeader("Accept", "*/*").addHeader("accept-encoding", "gzip, deflate") 
    

    하지만 쿠키는 현재 세션을 추적하기 위해 웹에서 사용해야합니다. API에서 필요하지 않아야합니다.

    관련 문제