Handling Authentication in Okhttp

前端 未结 2 556
闹比i
闹比i 2021-02-02 00:55

I\'m using OkHttp 2.3 with basic authentication requests, according to OKHttp docs, it automatically retries unauthenticated requests, but whenever I provide invali

相关标签:
2条回答
  • 2021-02-02 01:34

    Modified version of Traxdata's answer that works:

    protected Authenticator getBasicAuth(final String username, final String password) {
        return new Authenticator() {
            private int mCounter = 0;
    
            @Override
            public Request authenticate(Route route, Response response) throws IOException {
                Log.d("OkHttp", "authenticate(Route route, Response response) | mCounter = " + mCounter);
                if (mCounter++ > 0) {
                    Log.d("OkHttp", "authenticate(Route route, Response response) | I'll return null");
                    return null;
                } else {
                    Log.d("OkHttp", "authenticate(Route route, Response response) | This is first time, I'll try to authenticate");
                    String credential = Credentials.basic(username, password);
                    return response.request().newBuilder().header("Authorization", credential).build();
                }
            }
        };
    }
    

    Then you need to:

    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.authenticator(getBasicAuth("username", "pass"));
    retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    

    That's it.

    0 讨论(0)
  • 2021-02-02 01:37
    protected Authenticator getBasicAuth(final String username, final String password) {
        return new Authenticator() {
            private int mCounter = 0;
    
            @Override
            public Request authenticate(Proxy proxy, Response response) throws IOException {
                if (mCounter++ > 0) {
                    throw new AuthenticationException(
                            AuthenticationException.Type.INVALID_LOGIN, response.message());
                }
    
                String credential = Credentials.basic(username, password);
                return response.request().newBuilder().header("Authorization", credential).build();
            }
    
            @Override
            public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
                return null;
            }
        };
    }
    

    In my Authenticator I simply count the tries - after X tries, I throw an exception.

    0 讨论(0)
提交回复
热议问题