gpt4 book ai didi

java - 尝试获取 appDataFolder 中的文件列表时出现 GoogleAuthIOException

转载 作者:行者123 更新时间:2023-11-29 23:29:22 27 4
gpt4 key购买 nike

我正在尝试将应用程序数据库保存到 Google Drive 上的 Apps 文件夹中。我正在使用 Drive v3 API。几个月前一切正常,数据成功保存到用户 appData 文件夹。但是现在,我收到了 GoogleAuthIOException。我的应用在 Google 控制台(oAuth、Drive API)中具有所有必要的 API key

这是我的代码:

首先,向用户显示一个对话框,以获取他的帐户:

mCredential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Collections.singletonList(DriveScopes.DRIVE_APPDATA))
.setBackOff(new ExponentialBackOff());
//...
startActivityForResult(
mCredential.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER);
//...
if (resultCode == RESULT_OK && data != null &&
data.getExtras() != null) {
String accountName =
data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
mCredential.setSelectedAccountName(accountName);
}
}

接下来,我执行 AsyncTask,检查文件夹中的当前文件(如果需要,删除它)并将当前数据库保存在其中。

我的 AsyncTask 类的完整列表:

private class MakeRequestTask extends AsyncTask<Boolean, Void, Boolean> {
private com.google.api.services.drive.Drive mService;
File databasePath;
FileList fileList;

MakeRequestTask(GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.drive.Drive.Builder(
transport, jsonFactory, credential)
.setApplicationName(getString(R.string.app_name))
.build();
databasePath = BackupActivity.this.getDatabasePath(databaseName);
}


@Override
protected Boolean doInBackground(Boolean... params) {
if (params[0]) {
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setName(databaseOnGDName);
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
FileContent mediaContent = new FileContent("application/x-sqlite3", databasePath);
try {
fileList = mService.files().list().setSpaces("appDataFolder").execute(); //Here error appears
if (fileList.getFiles().size() == 0) {
mService.files().create(fileMetadata, mediaContent).execute();
} else {
for (com.google.api.services.drive.model.File file : fileList.getFiles()) {
mService.files().delete(file.getId()).execute();
}
mService.files().create(fileMetadata, mediaContent).execute();
}
return null;
} catch (Exception e) {
e.printStackTrace();
return true;
}
} else {
try {
fileList = mService.files().list().setSpaces("appDataFolder").execute();
for (com.google.api.services.drive.model.File file : fileList.getFiles()) {
if (file.getName().equals(databaseOnGDName)) {
File appDB = databasePath;
InputStream inputStream = mService.files().get(file.getId()).executeMediaAsInputStream();
OutputStream outputStream = new FileOutputStream(appDB);
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();
}
}
return false;
} catch (IOException e) {
e.printStackTrace();
return true;
}
}
}
}

来自 logcat 的错误:

2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:299)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at evansir.tarotdivinations.BackupActivity$MakeRequestTask.doInBackground(BackupActivity.java:291)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at evansir.tarotdivinations.BackupActivity$MakeRequestTask.doInBackground(BackupActivity.java:266)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at java.lang.Thread.run(Thread.java:764)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.zze.zza(Unknown Source:133)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.zzd.zza(Unknown Source:21)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.zzd.zza(Unknown Source:74)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.zzd.getToken(Unknown Source:3)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.zzd.getToken(Unknown Source:5)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.zzd.getToken(Unknown Source:7)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source:0)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:267)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:292)
2018-10-22 12:34:45.426 8931-9036/evansir.tarotdivinations W/System.err: ... 12 more

我如何从错误中看出 - 身份验证问题。但我所有的代码都来自 Java 的 Google 示例.我该如何解决这个错误?

理论上,我需要执行一些授权程序,但我没有找到适合我的情况

最佳答案

好吧,我终于找到了解决办法。

首先,当用户在此处选择他的帐户时:

startActivityForResult(
mCredential.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER);

我使用 setSelectedAccount(account, "package") 设置所选帐户,而不是 setSelectedAccountName(accountName)

然后我在我的 AsyncTask 中又添加了一个 catch,它创建了一个请求许可的对话框(如 official documentation 中所述):

        try {

fileList = mService.files().list().setSpaces("appDataFolder").execute();
//...
return null;
} catch (UserRecoverableAuthIOException e) {
e.printStackTrace();
errorCode = 1;
startActivityForResult(e.getIntent(), REQUEST_SIGNIN_REQUIRED);
return true;
}

关于java - 尝试获取 appDataFolder 中的文件列表时出现 GoogleAuthIOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52926124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com