3

I'm trying to implement a custom DataStoreFactory as mentioned in the docs here so that I can authenticate with Google APIs and store my access/refresh tokens in my database, but I can't find any documentation or examples of a custom implementation e.g.

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
             .setDataStoreFactory(new MyCustomDatastoreFactory() )
             .setAccessType("offline")
             .build();

If I implement the DataStoreFactory interface, it gives me one method to implement:

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 {
        ...
    }

}

The DataStore object has several methods that need to be implemented, but I'm not sure where and how exactly I need to use this to retrieve and store my credentials:

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;
    }
};

There is also DataStoreCredentialRefreshListener class, but do I need to implement that and associate it with my DataStoreFactory or DataStore so that it will automatically update my DB with new tokens?

RTF
  • 6,214
  • 12
  • 64
  • 132
  • you can try to use `MemoryDataStoreFactory` to simplify your work. – min Nov 08 '22 at 10:23
  • @min I never did actually figure this out, but as a workaround I ended up using `FileDataStoreFactory` loaded from the local file system. – RTF Nov 18 '22 at 10:53
  • I found a couple articles but I haven't tried them yet: https://medium.com/@dirkvranckaert/google-authentication-with-jpa-f1c78d386c45 https://mlescaille.medium.com/using-a-db-and-jpa-as-data-store-for-google-authentication-e1780b22e772 – user1689987 Dec 09 '22 at 18:54

0 Answers0