【问题标题】:Getting unauthorized error after setting the authorization token in the header of retrofit call在改造调用的标头中设置授权令牌后出现未经授权的错误
【发布时间】:2021-01-27 02:01:36
【问题描述】:

我正在使用带有 okhttp3 的改造 2。在这里我设置了我的令牌,但它仍然显示未经授权的错误。它没有进入下面代码的 try-catch 中。(即令牌没有被打印)

我的方法是使用单个文件进行改造服务,它将用于所有其他调用。

这是我的代码

public static Retrofit getClient() {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        clientBuilder.addInterceptor(loggingInterceptor);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiConstant.SERVER_API)
                .client(createOkHttpClient())
                .client(clientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        return retrofit;
    }

 private static OkHttpClient createOkHttpClient() {
        final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        if (isTokenRequired()) {
            if (!TextUtils.isEmpty(Profile.getToken())) {
                String token = Profile.getToken();
                httpClient.addInterceptor(
                        new Interceptor() {
                            @Override
                            public Response intercept(Interceptor.Chain chain) throws IOException {

                                Request newRequest = null;
                                try {
                                    System.out.println("token ==" + token);
                                     newRequest = chain.request().newBuilder()
                                            .header("Authorization", "Token " + token)
                                            .build();
                                    System.out.println("newRequest ===" + newRequest);

                                }  catch (Exception e) {
                                    e.printStackTrace();
                                }
                                return chain.proceed(newRequest);
                            }
                        });
            } else {
                throw new IllegalStateException("No Token");
            }
        }
        return httpClient.build();
    }

【问题讨论】:

    标签: java android android-studio retrofit2 rx-java2


    【解决方案1】:

    这是incorrect 的授权标头(没有这样的授权类型)。基于令牌的身份验证格式为 Bearer <TOKEN> 而不是 Token <TOKEN>

    还要检查您的拦截器是否已实际添加,它应该与指定的更正一起使用。

    【讨论】:

    • 我的授权标头只是令牌。
    • 单独试过了吗?另外,检查是否添加了拦截器。
    • 单独表示,我是否需要单独添加标头拦截器。我没找到你
    • 您使用了两个 OkHttp 构建器,一个具有 HttpLoggingInterceptor,另一个具有令牌拦截器。检查问题中的代码,有两个 client() 调用,其中第二个覆盖第一个(带有 Logging 的那个)
    • 是的,它会覆盖。所以你能告诉我一件事吗,如果我使用 HttpLoggingInterceptor 那么客户端调用不会按预期工作。那我该如何制作或打印日志
    【解决方案2】:

    删除 HttpLoggingInterceptor 后它工作了

    public static Retrofit getClient() {
            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
         
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstant.SERVER_API)
                    .client(createOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
    
            return retrofit;
        }
    
     private static OkHttpClient createOkHttpClient() {
            final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    
            if (isTokenRequired()) {
                if (!TextUtils.isEmpty(Profile.getToken())) {
                    String token = Profile.getToken();
                    httpClient.addInterceptor(
                            new Interceptor() {
                                @Override
                                public Response intercept(Interceptor.Chain chain) throws IOException {
    
                                    Request newRequest = null;
                                    try {
                                        System.out.println("token ==" + token);
                                         newRequest = chain.request().newBuilder()
                                                .header("Authorization", "Token " + token)
                                                .build();
                                        System.out.println("newRequest ===" + newRequest);
    
                                    }  catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    return chain.proceed(newRequest);
                                }
                            });
                } else {
                    throw new IllegalStateException("No Token");
                }
            }
            return httpClient.build();
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 2016-07-02
      • 2023-03-03
      • 2023-03-05
      • 2021-11-21
      • 2021-06-20
      相关资源
      最近更新 更多