gpt4 book ai didi

android - Google Drive API - 名称不能为空 : null (But I had passed valid account name to GoogleAccountCredential)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:22 26 4
gpt4 key购买 nike

最近,我有访问 Google Drive 的 Android 代码。我使用的是 Google APIs Client Library for Java 而不是 Google Play services client library

private static GoogleCloudFile searchFromGoogleDrive(Drive drive, String qString, HandleUserRecoverableAuthIOExceptionable h, PublishProgressable p) {
try {
Files.List request = drive.files().list().setQ(qString);

do {
if (p.isCancelled()) {
return null;
}

FileList fileList = request.execute();

如果我使用 targetSdkVersion 21,该代码可以 100% 正常工作数年。

最近,我将我的应用程序迁移到 targetSdkVersion 23,Google Drive 相关代码的更改为 0。

但是,代码在 FileList fileList = request.execute(); 处神秘崩溃,但出现以下异常。

使用 1.18.0-rc 时的错误日志

Process: org.yccheok.jstock.gui, PID: 30317
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: the name must not be empty: null
at android.accounts.Account.<init>(Account.java:48)
at com.google.android.gms.auth.zzd.getToken(Unknown Source)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at org.yccheok.jstock.gui.Utils.searchFromGoogleDrive(Utils.java:208)
at org.yccheok.jstock.gui.Utils._loadFromGoogleDrive(Utils.java:277)
at org.yccheok.jstock.gui.Utils.loadFromGoogleDrive(Utils.java:344)
at org.yccheok.jstock.gui.LoadFromCloudTask.doInBackground(LoadFromCloudTask.java:23)
at org.yccheok.jstock.gui.LoadFromCloudTask.doInBackground(LoadFromCloudTask.java:9)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

如果我用谷歌搜索,我会得到 Google Drive on android error: java.lang.IllegalArgumentException: the name must not be empty: null ,它指出帐户名称未传递给凭据对象。

但是,我非常确定我的凭据对象是通过有效的帐户名(格式为 xxx@gmail.com)传递的

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case RequestCode.REQUEST_ACCOUNT_PICKER_LOAD_FROM_CLOUD:
if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
Log.i("CHEOK", "accountName = " + accountName);
if (accountName != null) {
JStockApplication.instance().getJStockOptions().setUsername(accountName);
Utils.getGoogleAccountCredential().setSelectedAccountName(accountName);

实用程序.java

public static GoogleAccountCredential getGoogleAccountCredential() {
return googleAccountCredential;
}

private static final GoogleAccountCredential googleAccountCredential = GoogleAccountCredential.usingOAuth2(JStockApplication.instance(),
Arrays.asList(
DriveScopes.DRIVE_APPDATA,
// Legacy. Shall be removed after a while...
DriveScopes.DRIVE
)
);

private Drive getDriveService() {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), Utils.getGoogleAccountCredential()).build();
}

此外,我非常确定我的 OAuth 2.0 可以通过 Google Developers Console 正确使用(对于调试 keystore 和生产 keystore )最近,Android 6 引入了运行时权限。我在想,这个异常是否与此有关?

我进一步尝试使用最新的库版本 1.21.0。但是,我仍然得到类似的错误日志

使用 1.21.0 时的错误日志

java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: the name must not be empty: null
at android.accounts.Account.<init>(Account.java:48)
at com.google.android.gms.auth.zzd.getToken(Unknown Source)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at org.yccheok.jstock.gui.Utils.searchFromGoogleDrive(Utils.java:208)
at org.yccheok.jstock.gui.Utils._loadFromGoogleDrive(Utils.java:277)
at org.yccheok.jstock.gui.Utils.loadFromGoogleDrive(Utils.java:344)
at org.yccheok.jstock.gui.LoadFromCloudTask.doInBackground(LoadFromCloudTask.java:23)
at org.yccheok.jstock.gui.LoadFromCloudTask.doInBackground(LoadFromCloudTask.java:9)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

我进一步测试的更新

以上 2 个错误日志是在我使用 Nexus 5,Android 版本 6.0.1 进行测试时生成的

但是,如果我使用 Nexus 4、Android 版本 5.1.1 进行相同的测试,则不会出现错误! 1.18.0-rc 和 1.21.0 都工作正常

因此,崩溃似乎与 Android 版本密切相关。

最佳答案

看起来 Drive API Client Library for Java 调用了 GoogleAuthUtil.getToken(),这需要 GET_ACCOUNTS 权限。您需要在您的 list 中拥有该权限并在运行时根据需要请求它。

更新:使用 Google Drive REST v3 API,不需要 GET_ACCOUNTS 权限。相反,需要电子邮件许可。您可以通过调用 GoogleSignInOptions.Builder.requestEmail 请求它。

关于android - Google Drive API - 名称不能为空 : null (But I had passed valid account name to GoogleAccountCredential),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34639890/

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