我正在尝试使用Retrofit在我的Android应用中实现OAuth2。我已经创建了Authenticate和Refresh方法,但在获取请求失败时遇到问题,401在刷新之前再次尝试。
My Rest Adapter如下
this.linearLayout_top5classes..post(new Runnable() {
public void run() {
this.linearLayout_top5classes..removeView(view);
}
});
我在authenticate方法中的目标是调用refresh方法,然后使用新的access_token发送原始请求的更新版本。
刷新方法如下
private Retrofit getRestAdapter() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, final okhttp3.Response response) throws IOException {
refresh(new CallBack<UserAuthentication>() {
@Override
public void onResult(UserAuthentication result) {
okhttp3.Response modifiedResponse = response;
modifiedResponse.request().newBuilder().header("access_token", result.getAuthToken()).build();
}
@Override
public void onError(Throwable error) {
Log.d("Error", "Couldn't refresh");
}
});
return null;
}
});
if (restAdapter == null) {
restAdapter = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(builder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return restAdapter;
}
任何建议都将不胜感激。