我正在应用程序中使用改造2。在申请中有授权。一切正常,但还有一个问题。我有access_token
和refresh_token
。当我登录应用程序时,我得到了令牌。当我的access_token
过期时,我可以获取带有刷新令牌的新令牌。如果刷新令牌也过期,则我再次授权获取新令牌。
在我的情况下,当改装需要发送呼叫以更新令牌时,我会多次发送数据。可能是什么原因造成的?
private ApiClient(String endpoint) {
OkHttpClient.Builder client = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS);
client.addInterceptor(new Interceptor() {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.header("Cache-Control", "public, max-age=0")
.build();
return chain.proceed(request);
}
});
supportopApi = new Retrofit.Builder()
.baseUrl(endpoint)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(SupportopApi.class);
}
public static synchronized void initializeInstance(String endpoint) {
if (instance == null) {
instance = new ApiClient(endpoint);
}
}
public static synchronized ApiClient getInstance() {
if (instance == null) {
throw new IllegalStateException("PentairAPIClient has not been initialized.");
}
return instance;
}
我不想在此处添加大代码,因此也可以添加更新部分。
public void updateToken() {
//Perform this call if access token is expired
final ApiClient apiClient = ApiClient.getInstance();
SupportObjToken supportObjToken = new SupportObjToken();
//Here I'm setting my user information for request
supportObjToken.setGrantType("refresh_token");
supportObjToken.setClientId(SharedPreferencesManager.getInstance().getUserData().getClientId());
supportObjToken.setClientSecret(SharedPreferencesManager.getInstance().getUserData().getClientSecret());
supportObjToken.setRefreshToken(SharedPreferencesManager.getInstance().getUserData().getRefreshToken());
//This request calls multiple times (2-3 times or more)
Call<RefreshTokenActivation> newToken = apiClient.newToken(supportObjToken);
newToken.enqueue(new Callback<RefreshTokenActivation>() {
@Override
public void onResponse(Call<RefreshTokenActivation> call, Response<RefreshTokenActivation> response) {
if (response.isSuccessful()) {
String newAccessToken = response.body().getAccessToken();
String newRefreshToken = response.body().getRefreshToken();
Here I'm saving my updated access_token
UserData userData = SharedPreferencesManager.getInstance().getUserData();
SharedPreferencesManager.getInstance().removeUser();
userData.setAccessToken(newAccessToken);
userData.setRefreshToken(newRefreshToken);
SharedPreferencesManager.getInstance().setUser(userData);
tokenUpdaterCallback.updateToken();
} else {
if (response.code() == 401) {
//Perform this call if refresh token is expired
//This request is called multiple times too
Call<TokenActivation> token = apiClient.getToken("password",
SharedPreferencesManager.getInstance().getUserData().getClientId(),
SharedPreferencesManager.getInstance().getUserData().getClientSecret(),
SharedPreferencesManager.getInstance().getUserData().getEmail(),
SharedPreferencesManager.getInstance().getUserData().getPassword());
token.enqueue(new Callback<TokenActivation>() {
@Override
public void onResponse(Call<TokenActivation> call, Response<TokenActivation> response) {
if (response.isSuccessful()) {
String access_token = response.body().getAccessToken();
String refresh_token = response.body().getRefreshToken();
UserData userData = SharedPreferencesManager.getInstance().getUserData();
SharedPreferencesManager.getInstance().removeUser();
userData.setAccessToken(access_token);
userData.setRefreshToken(refresh_token);
SharedPreferencesManager.getInstance().setUser(userData);
//tokenUpdaterCallback goes to main class and repeats the authorization request
tokenUpdaterCallback.updateToken();
}
}
@Override
public void onFailure(Call<TokenActivation> call, Throwable t) {
Toast.makeText(context, "An error occurred", Toast.LENGTH_SHORT).show();
}
});
}
}
}
@Override
public void onFailure(Call<RefreshTokenActivation> call, Throwable t) {
Toast.makeText(context, "Response is not successful", Toast.LENGTH_SHORT).show();
}
});
}
可能是什么问题?谢谢。