¿Cómo implementar una fábrica de DataStore personalizada para su uso con las API de Google utilizando OAuth2?
Estoy tratando de implementar una DataStoreFactory
personalizada según se menciona en la documentación aquí para poder autenticarme con las APIs de Google y almacenar mis tokens de acceso / actualización en mi base de datos, pero no puedo encontrar ninguna documentación o ejemplos de una implementación personalizada por ejemplo:
// Construir el flujo e iniciar la solicitud de autorización del usuario.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new MyCustomDatastoreFactory() )
.setAccessType("offline")
.build();
Si implemento la interfaz DataStoreFactory
, me da un método para implementar:
import java.io.IOException;
import java.io.Serializable;
import com.google.api.client.util.store.DataStore;
import com.google.api.client.util.store.DataStoreFactory;
public class MyCustomDatastoreFactory implements DataStoreFactory {
@Override
public <V extends Serializable> DataStore<V> getDataStore(String arg0) throws IOException {
...
}
}
El objeto DataStore
tiene varios métodos que deben ser implementados, pero no estoy seguro de dónde y cómo exactamente necesito usar esto para recuperar y almacenar mis credenciales:
new DataStore<Serializable>() {
@Override
public DataStore<Serializable> clear() throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean containsKey(String arg0) throws IOException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsValue(Serializable arg0) throws IOException {
// TODO Auto-generated method stub
return false;
}
@Override
public DataStore<Serializable> delete(String arg0) throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public Serializable get(String arg0) throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public DataStoreFactory getDataStoreFactory() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getId() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isEmpty() throws IOException {
// TODO Auto-generated method stub
return false;
}
@Override
public Set<String> keySet() throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public DataStore<Serializable> set(String arg0, Serializable arg1) throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public int size() throws IOException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Collection<Serializable> values() throws IOException {
// TODO Auto-generated method stub
return null;
}
};
También hay una clase DataStoreCredentialRefreshListener
, ¿pero necesito implementar eso y asociarlo con mi DataStoreFactory
o DataStore
para que actualice automáticamente mi base de datos con nuevos tokens?
davy.ai
Para implementar una fábrica de
DataStore
personalizada, primero cree una clase que implemente la interfazDataStoreFactory
. Esta clase debe tener un métodogetDataStore
que devuelva un objetoDataStore
. Dentro del métodogetDataStore
, puede crear una instancia de su claseDataStore
personalizada que implemente los métodos necesarios.Por ejemplo:
A continuación, cree una clase que implemente la interfaz
DataStore
. Esta clase debe implementar los métodos necesarios para almacenar y recuperar sus tokens de acceso/actualización.Por ejemplo:
Para usar su fábrica de
DataStore
personalizada, pase una instancia de ella al métodosetDataStoreFactory
al construir su objetoGoogleAuthorizationCodeFlow
. Por ejemplo:También necesitará implementar la clase
DataStoreCredentialRefreshListener
y asociarla con su fábrica oDataStore
. Este listener es responsable de actualizar automáticamente su base de datos con nuevos tokens cuando se refrescan.Por ejemplo:
Luego, al construir su objeto
GoogleAuthorizationCodeFlow
, pase una instancia de su listener personalizado al métodosetCredentialDataStore
. Por ejemplo:java
</string,>MyCustomDatastore<storedcredential> datastore = new MyCustomDatastore<>();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new MyCustomDatastoreFactory())
.setCredentialDataStore(datastore)
.setAccessType("offline")
.addRefreshListener(new MyCustomCredentialRefreshListener(datastore))
.build();