@Override protected void onPostExecute(AccessToken accessToken) { mAccessTokenTask = null; final ManagedChannel channel = new OkHttpChannelProvider() .builderForAddress(HOSTNAME, PORT) .nameResolverFactory(new DnsNameResolverProvider()) .intercept(new GoogleCredentialsInterceptor(new GoogleCredentials(accessToken) .createScoped(SCOPE))) .build(); mApi = SpeechGrpc.newStub(channel); // Schedule access token refresh before it expires if (mHandler != null) { mHandler.postDelayed(mFetchAccessTokenRunnable, Math.max(accessToken.getExpirationTime().getTime() - System.currentTimeMillis() - ACCESS_TOKEN_FETCH_MARGIN, ACCESS_TOKEN_EXPIRATION_TOLERANCE)); } }