How to implement a custom DataStoreFactory for use with Google APIs using OAuth2?

1k views Asked by At

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?

0

There are 0 answers