我正在尝试将Google云端硬盘集成到我的Android应用中,以便我可以将文件存储在用户的云端硬盘中。我已在App Console中启用了Drive SDK和Drive API,并使用我的SHA1指纹设置了我的Oauth 2 ClientID。据我所知,谷歌的一切看起来都不错。
这是初始化我的Drive对象的代码。
private boolean initializeService(String accountName) {
if(service != null) {
return true;
}
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE);
mAuthIntent = null;
if(accountName != null) {
try {
credential.setSelectedAccountName(accountName);
Log.d("DriveHelper", credential.getToken());
service = getDriveService(credential);
} catch(Exception ex) {
if(ex instanceof UserRecoverableAuthException) {
UserRecoverableAuthException authException = (UserRecoverableAuthException)ex;
mAuthIntent = authException.getIntent();
mErrorCode = CloudHelper.CONNECTION_AUTHENTICATE;
((Activity)mContext).startActivityForResult(mAuthIntent, REQUEST_AUTHORIZATION);
} else {
Log.e("DriveCloudHelper", "Error retrieving auth token", ex);
}
return false;
}
getAppFolder();
return mFileLocation != null;
}
Activity activity = (Activity)mContext;
activity.startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
return false;
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.setApplicationName("Drive Helper")
.build();
}
这些方法在后台线程上调用并且工作正常,出现帐户选择器,出现授权窗口,一切正常。我的getAppFolder
方法查找特定文件夹,如果不存在则创建它(没有任何问题)(在同一个后台线程上)。
但是,当我将文件上传到云端硬盘时,我在新后台线程上尝试的第一个操作会导致UserRecoverableAuthException
,但是当我调用getIntent
时会发生新的异常,因为没有意图已提供,详细消息为AppDownloadRequired
。
这是生成异常的方法。
private String getIdFromFolder(String location, String name, boolean create) {
try {
String maskedName = name.replaceAll("/", "");
String query = "title contains '" + maskedName + "' and '" + location + "' in parents";
FileList list = service.files().list().setMaxResults(1).setQ(query).execute();
List<File> files = list.getItems();
if (files.isEmpty()) {
return create ? createSubFolder(location, name) : null;
}
return files.get(0).getId();
} catch (IOException e) {
// Eat the exception here
Log.w("DriveCloudHelper", e);
}
return create ? createSubFolder(location, name) : null;
}
这是堆栈跟踪:
com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:222)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.getIdFromFolder(DriveCloudHelper.java:269)
at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.uploadDatabaseToCloud(DriveCloudHelper.java:405)
at net.wishfullthinking.groceryhelper.interfaces.DriveCloudHelper.synchFileToCloud(DriveCloudHelper.java:368)
at net.wishfullthinking.groceryhelper.interfaces.CloudHelper.mergeCloudStoreCollection(CloudHelper.java:472)
at net.wishfullthinking.groceryhelper.interfaces.CloudHelper.access$10(CloudHelper.java:454)
at net.wishfullthinking.groceryhelper.interfaces.CloudHelper$4.run(CloudHelper.java:444)
at java.lang.Thread.run(Thread.java:856)
Caused by: com.google.android.gms.auth.UserRecoverableAuthException: AppDownloadRequired
at com.google.android.gms.auth.GoogleAuthUtil.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:192)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
... 11 more
答案 0 :(得分:2)
尝试在mContext.getApplicationContext()
来电中使用GoogleAccountCredential.usingOAuth2
,而不是mContext
,因为其他地方似乎已修复similar issues。