[英]Getting unauthorized error after setting the authorization token in the header of retrofit call
I am using retrofit2 with okhttp3.我正在使用带有 okhttp3 的改造 2。 Here I have set my token but still, it shows unauthorized error.
在这里我已经设置了我的令牌,但它仍然显示未经授权的错误。 It did not go inside the try-catch of the below code.(ie) the token not get printed)
它没有进入下面代码的 try-catch 中。(即,没有打印令牌)
My approach is to use a single file for retrofit service and it will be used in all other calls.我的方法是使用单个文件进行改造服务,它将用于所有其他调用。
Here is my code这是我的代码
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();
}
It's the Authorization Header which is incorrect (there is no such Authorization type).这是不正确的授权标头(没有这样的授权类型)。 Token based Authentication is of the format
Bearer <TOKEN>
and not Token <TOKEN>
基于令牌的认证的格式为
Bearer <TOKEN>
而不是Token <TOKEN>
Also check if your Interceptor is actually added, which should work with the correction specified.还要检查您的拦截器是否实际添加,这应该与指定的更正一起使用。
It worked after removing the HttpLoggingInterceptor它在删除 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.