문제

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);
도움이 되었습니까?

해결책

You can't access assets as you would access regular files, since these files are bundled with the application. That's why new File(PRIVATE_KEY) doesn't work, and there is no path you can give that would make it work.

What you could do is get an InputStream for that file :

AssetManager assetManager = context.getAssets();
InputStream input = assetManager.open(PRIVATE_KEY);

If you need to access it as a File, you could copy it to the internal storage of your app the first time your application is launched. I'm not sure that's the best solution (perhaps you don't want to store the private key on the device's internal storage for security reasons), but it should work. Then you can access it from the context.getFilesDir () folder.

InputStream fis = assetManager.open(PRIVATE_KEY);
FileOutputStream fos = openFileOutput(PRIVATE_KEY, Context.MODE_PRIVATE);
byte buf[] = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
  fos.write(buf, 0, len);
}
fis.close();
fos.close();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top