I have an android app, that should use google fusion tables. I'm using a google service account and have to get the path of my xxxxxxxxxxxprivatekey.p12.
public class CredentialProvider {
...
private static String PRIVATE_KEY = "xxxxxxxxxxxprivatekey.p12";
...
public static GoogleCredential getCredential() throws IOException, GeneralSecurityException {
return getCredential(Arrays.asList(SCOPES), SERVICE_ACCOUNT_EMAIL, new File(PRIVATE_KEY), HTTP_TRANSPORT, JSON_FACTORY);
}
...
}
The code wants to make a new File out of the PRIVATE_KEY
path. I've tried various paths but every time I'm getting a FileNotFoundException
and open failed: ENOENT (No such file or directory)
.
I have read something about the assets folder, but I don't know how to get that work with the getCredential method.
- Where I have to put my private key in my android project and
- how has the PRIVATE_KEY path to look like and
- how I get "new File(PRIVATE_KEY)" work?
Thanks ;)
EDIT:
now I'm overriding GoogleCredential.Builder
to create my own setServiceAccountPrivateKeyFromP12File(InputStream p12File)
like in your link and it seems to work fine. But in getCredential()
refreshToken()
is called and crashes in a NetworkOnMainThreadException.
I've read, that I should use AsyncTask for it. Can you give me a hint, where I have to put that AsyncTask and what should be inside doInBackground()
and what inside onPostExecute()
or any method?
Here is the code of getCredential()
. It crashes in refreshToken()
with a NetworkOnMainThreadException:
public static GoogleCredential getCredential(List<String> SCOPE, String SERVICE_ACCOUNT_EMAIL,
InputStream inputStreamFromP12File, HttpTransport HTTP_TRANSPORT, JsonFactory JSON_FACTORY)
throws IOException, GeneralSecurityException {
// Build service account credential.
MyGoogleCredentialBuilder builder = new MyGoogleCredentialBuilder();
builder.setTransport(HTTP_TRANSPORT);
builder.setJsonFactory(JSON_FACTORY);
builder.setServiceAccountId(SERVICE_ACCOUNT_EMAIL);
builder.setServiceAccountScopes(SCOPE);
builder.setServiceAccountPrivateKeyFromP12File(inputStreamFromP12File);
GoogleCredential credential = builder.build();
credential.refreshToken();
return credential;
}
EDIT2:
Finally, I solved it that way:
private static class RefreshTokenTask extends AsyncTask<GoogleCredential, Void, Void> {
@Override
protected Void doInBackground(GoogleCredential... params) {
try {
params[0].refreshToken();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
and in my getCredential method:
new RefreshTokenTask().execute(credential);