Reputation: 390
I'm trying to integrate Google Drive into my Android app so that I can store files in a user's Drive. I've enabled both Drive SDK and Drive API in the App Console and set up my Oauth 2 ClientIDs with my SHA1 fingerprint. As far as I can tell, everything looks good on Google's end.
Here's the code that initializes my Drive object.
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();
}
These methods are called on a background thread and work just fine, the Account Picker appears, the Authorization window appears, all good. My getAppFolder
method looks for a specific folder and creates it if not present without any issue (on the same background thread).
However, when I go to upload a file to Drive, the first operation I try on a new background thread causes a UserRecoverableAuthException
, but a new exception occurs when I call getIntent
because there's no Intent provided, and the detail message is AppDownloadRequired
.
Here's the method that's generating the exception.
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;
}
And here's the stacktrace:
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
Upvotes: 0
Views: 1080
Reputation: 199880
Try using mContext.getApplicationContext()
in your GoogleAccountCredential.usingOAuth2
call rather than mContext
as that seems to have fixed similar issues elsewhere.
Upvotes: 2