简体   繁体   中英

Google Authentication can't find .p12 file

I am trying to make an app that will edit my google spreadsheet.

But i am having some problems with the authentication part. Right now my code for the authentication looks like this.

    URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    File p12 = new File("./Key.p12");

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    String [] SCOPESArray = {"https://spreadsheets.google.com/feeds","https://spreadsheets.google.com/feeds/spreadsheets/private/full"};
    final List SCOPES = Arrays.asList(SCOPESArray);


    GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId("XXXXXXXXXXXXXgserviceaccount.com")
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountPrivateKeyFromP12File(p12)
                .build();

But this when i try to run this the app crashes on the Google credentials builder.This is the error i recieve:

java.io.FileNotFoundException: /./Key: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder.setServiceAccountPrivateKeyFromP12File(GoogleCredential.java:670)
at com.example.XXXX.UpdateSpreadsheet.authenticate(UpdateSpreadsheet.java:57)
at com.example.XXXX.UpdateSpreadsheet.doInBackground(UpdateSpreadsheet.java:124)
at com.example.XXXX.UpdateSpreadsheet.doInBackground(UpdateSpreadsheet.java:39)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    at libcore.io.IoBridge.open(IoBridge.java:393)
    ... 11 more

I have tried different paths for the path of the p12 file but none of them work. I tried "./Key.p12", "/Key.p12", "Key.p12" and also the full path to the file but that also didn't work.

It says that it is not able to open it but i have placed it in the project directory like "Projectname/Key.p12". I have no idea what i am doing wrong if somebody could help me that would be great.

尝试像这样创建p12 File对象:

File p12 = new File(this.getClass().getResource("/Key.p12").toURI());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM