簡體   English   中英

在改造調用的標頭中設置授權令牌后出現未經授權的錯誤

[英]Getting unauthorized error after setting the authorization token in the header of retrofit call

我正在使用帶有 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();
    }

這是不正確的授權標頭(沒有這樣的授權類型)。 基於令牌的認證的格式為Bearer <TOKEN>而不是Token <TOKEN>

還要檢查您的攔截器是否實際添加,這應該與指定的更正一起使用。

它在刪除 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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM