我有一个连接到Okhttp3客户端的身份验证器,该客户端在收到401响应时被成功调用。在身份验证器中,我想使用刷新令牌对用户进行身份验证。我正在使用IBM AppId进行身份验证。
private Authenticator getAuthenticator() {
return new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
// code to authenticate with refresh token
return null;
}
};
}
我有以下代码用于使用刷新令牌进行身份验证:
AppID.getInstance().signinWithRefreshToken(getApplicationContext(), refreshTokenString, new AuthorizationListener() {
@Override
public void onAuthorizationFailure(AuthorizationException exception) {
//Exception occurred
}
@Override
public void onAuthorizationCanceled() {
//Authentication canceled by the user
}
@Override
public void onAuthorizationSuccess(AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
//User authenticated
}
});
现在您可以看到它是异步请求,因此我无法将此代码放入身份验证器中,因为该方法将在调用onAuthorizationSuccess()之前返回。另外,AppId没有我可以使用的同步请求类型。您能否指出我如何在Authenticator类中使用此代码。请帮我解决这个问题。
答案 0 :(得分:2)
我对此有解决方案。请尝试以下代码。
使类如下: TokenAuthenticator.java
import android.content.Context;
import android.support.annotation.Nullable;
import com.dmlllc.insideride.common.Preferences;
import com.dmlllc.insideride.model.AccessToken;
import com.dmlllc.insideride.restModel.RestResponse;
import com.dmlllc.insideride.restModel.requestModel.AccessTokenReq;
import java.io.IOException;
import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
public class TokenAuthenticator implements Authenticator {
private Context context;
public TokenAuthenticator(Context context) {
this.context = context;
}
@Nullable
@Override
public Request authenticate(Route route, Response response) throws IOException {
// Refresh your access_token using a synchronous api request
AccessTokenReq accessTokenReq = new AccessTokenReq(Preferences.getPreferenceString(context, Preferences.USERNAME_FOR_TOKEN, ""),
Preferences.getPreferenceString(context, Preferences.PASSWORD_FOR_TOKEN, ""));
try {
retrofit2.Response<RestResponse<AccessToken>> tokenResponse = Global.initRetrofit(context).getAccessToken(accessTokenReq).execute();
if (tokenResponse.body() != null) {
if (tokenResponse.body().getResStatus().equals("success")) {
SessionManager sessionManager = new SessionManager(context);
sessionManager.storeToken(tokenResponse.body().getResults().getYourAccessToken());
Preferences.setPreferenceString(context, Preferences.ACCESS_TOKEN, tokenResponse.body().getResults().getYourAccessToken());
}
}
}catch (Exception e){
e.printStackTrace();
}
// Add new header to rejected request and retry it
return response.request().newBuilder()
.header("Authorization", Preferences.getPreferenceString(context, Preferences.ACCESS_TOKEN, ""))
.build();
}
}
然后像下面这样在改造方法中使用此类:
public static RestApi initRetrofit(Context context) {
// For logging request & response (Optional)
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
TokenAuthenticator tokenAuthenticator = new TokenAuthenticator(context);
OkHttpClient client = new OkHttpClient.Builder()
.authenticator(tokenAuthenticator)
.addInterceptor(loggingInterceptor)
.connectTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit.create(RestApi.class);
}
希望它也会为您提供帮助。 :) 快乐编码... !!!
答案 1 :(得分:0)