尝试创建GoogleCredential时出现java.io.FileNotFoundException

时间:2017-05-22 21:09:35

标签: java android google-bigquery

我正在尝试在我的Android应用中运行此代码:

    public Bigquery createAuthorizedClient() throws IOException {
    final List SCOPE = Arrays.asList("https://www.googleapis.com/auth/bigquery");
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential();
    try{
        File file = new File("C:\\testprojectbigquery\\UkrBikeApp-ff55878cb577.p12");
        credential = new GoogleCredential.Builder().setTransport(transport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId("bigquerymain@ukrbikeapp.iam.gserviceaccount.com")
                .setServiceAccountScopes(SCOPE)
                .setServiceAccountPrivateKeyFromP12File(file)
                .build();
    }
    catch (Exception e){
        System.out.println("e = " + e);
    }


    if (credential.createScopedRequired()) {
        credential = credential.createScoped(BigqueryScopes.all());
    }

    return new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName("Bigquery Samples")
            .build();
}

但每次我得到:

  

java.io.FileNotFoundException

然后我尝试创建InputStream,但后来没有改变情况:

        AssetManager am = getAssets();
        InputStream inputStream = am.open("C:\\testprojectbigquery\\UkrBikeApp-ff55878cb577.p12");
        File file = createFileFromInputStream(inputStream);

1 个答案:

答案 0 :(得分:1)

您正在为Android设备编码,并且android没有C驱动器!要读取此文件,必须将文件放入项目的assets文件夹,然后从该文件夹中读取。这是example of reading assets