We are developing a game using PlayN framework and we'll have a global leaderboard stored in Google Cloud Datastore. We acquired the private key needed for accessing the database, it's in a .p12 file, so I assume it's in PKCS#12 format. Right now we added this file to our game project's assets. (The structure of our projects is the standard hierarchy generated by the Maven archetype). It seems that we have two choices:
1: Somehow create a PrivateKey object and supply that:
// Code copied from DataStoreHelper Source
// Note; One of the setaccountprivatekeymethods must be used.
public Credential getDatastoreCredentials(String accountId) throws GeneralSecurityException, IOException
{
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = new JacksonFactory();
return new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(accountId)
.setServiceAccountScopes(DatastoreOptions.SCOPES)
.setServiceAccountPrivateKey(new PrivateKey ???) // <- You cannot just construct PrivateKey
.build();
}
But that seems to be cumbersome: Creating PrivateKey Object from PKCS12 We haven't found a solution yet.
2: The better way would be to somehow get hold of the full path of the PKCS#12 file in the assets in a platform independent way:
// Code copied from DataStoreHelper Source
// Note; One of the setaccountprivatekeymethods must be used.
public Credential getDatastoreCredentials(String accountId) throws GeneralSecurityException, IOException
{
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = new JacksonFactory();
return new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(accountId)
.setServiceAccountScopes(DatastoreOptions.SCOPES)
.setServiceAccountPrivateKeyFromP12File(new File(???)) // <- acquire file path
.build();
}
We get exception in this case, the variations to get the file path did not work. Normally you access files in the assets by importing playn.core.PlayN.assets, and then calling assets(). For example loading an image is simply Image bgImage = assets().getImage("images/bg.png"); while loading a JSON data is a little longer:
assets().getText(jsonPath, new Callback<String>() {
@Override
public void onSuccess(String json) {
try {
parseJson(images, sprite, json);
....
But in our case we'd need the full path (?) of the file. We will target HTML5/CSS/JS and Android platform first, but then plan to cover more platforms. This looks like Android specific: How to get the path to the Android assets folder in the application package