Retrofit2 (interceptor) + GoogleApiClient how to update token

What is the best way to have a valid IdToken in every request?

My first bet will be the okhttpclient interceptor, which adds a token to each request. But I do not know how to get a valid token inside the interceptor.

The GoogleApiClient documentation suggests calling silentSignIn(GoogleApiClient)before each request to get a valid token. The problem is that I do not have access to the currently connected googleapiclient inside the interceptor.

+4
source share
2 answers

, , . .

  public class SessionData {

        private static String sessionId;

    public static String getSessionId() {
        return sessionId;
    }

    public static void setSessionId(String sessionId) {
        SessionData.sessionId = sessionId;
    }
    }

IdToken SDK Google (, ).

SessionData.setSessionId(yourCurrentToken);

, Retrofit.Builder, ( , okhttp).

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

.

public class RestClient implements Interceptor {

    public RestClient() {

        OkHttpClient httpClient = new OkHttpClient();
        // add your other interceptors …
        // add logging as last interceptor

        httpClient.interceptors().add(this);  // <-- this adds the header with the sessionId to every request

        Retrofit restAdapter = new Retrofit.Builder()
                .baseUrl(RestConstants.BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient)
                .build();
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();

        if (SessionData.getSessionId() != null) {
            Request newRequest = originalRequest.newBuilder()
                    .header("sessionId", SessionData.getSessionId())
                    .build();
            return chain.proceed(newRequest);
        }
        return chain.proceed(originalRequest);

    }
}
0

GoogleApiClient Auth.GOOGLE_SIGN_IN_API, GoogleApiClient, / . Google autoManage , GoogleApiClient FragmentActivity. , UI, . :

private static final GoogleSignInOptions sGso =
   new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(SERVER_CLIENT_ID)
        .requestEmail()
        .build();

// In your interceptor code:
GoogleApiClient apiClient = new GoogleApiClient.Builder(mContext)
    .addApi(Auth.GOOGLE_SIGN_IN_API, sGso)
    .build();

try {
    ConnectionResult result = apiClient.blockingConnect();
    if (result.isSuccess()) {
        GoogleSignInResult googleSignInResult =
            Auth.GoogleSignInApi.silentSignIn(apiClient).await();
        if (googleSignInResult.isSuccess) {
            String idToken = googleSignInResult.getIdToken();
            // set to header and pass to your server
        }
        ...
    }
} finally {
    apiClient.disconnect();
}
0

Source: https://habr.com/ru/post/1622489/


All Articles