I've been working on an Android App that uses Google Drive API. It was originally build from the quickstart example here. The simplified sequence of API calls (with proper error handling not shown here) is:
GoogleAccountCredential cred = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE_FILE);
cred.setSelectedAccountName("...");
Drive drvSvc = new Drive.Builder (AndroidHttp.newCompatibleTransport(), new GsonFactory(), cred).build();
FileList gooLst = drvSvc.files().list().setMaxResults(MAX_DOWN).setQ(_rqst).execute();
It has been working fine and I am just about to release my App. But suddenly, after Drive API update, I'm getting a warning
The method usingOAuth2(Context, String, String...) from the type GoogleAccountCredential is deprecated
What's happening? Is there another way to obtain credentials? Is there an edited version of quickstart example available anywhere?
Thanks in advance for any clarification. sean
So, the simple answer to my own question is
replace:
GoogleAccountCredential crd = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE_FILE);
with
GoogleAccountCredential crd = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE));
I'm guessing you got to this exception by following the code example from Google Drive QuickStart. If so you'll might find the following things I had to modify interesting.
The documentation uses the old way of adding libraries to an Android project. This will fail when executed with the latest ADT. You will be able to compile and upload to the device/emulator but on execution you'll get a NoClassDefFoundError.
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/AccountPicker;
To fix this you should copy paste the google-play-services.jar
file to the libs
folder instead.
Up to the next error. I then received an IllegalStateException
with the instructions to add a meta-tag in the manifest holding the google-play-services version.
So within the application tag of the manifest add:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
And in one of the resource files (res/values/a-file-here.xml
) add the following:
<integer name="google_play_services_version">4030500</integer>
In my case the lib matched this version. If you enter the wrong version here you'll get an error showing the proper version to specify. So make sure to check output.
Finally I got a prompt for oauth in the app just to find out the example app is still missing a permission. The error for reference:
java.io.FileNotFoundException: /storage/emulated/0/Pictures/IMG_20131211_110629.jpg: open failed: EACCES (Permission denied)
Next to the permissions listed within the example:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
Also add the WRITE_EXTERNAL_STORAGE permission in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If you get an com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
exception with in the root cause somewhere a description Unknown
you should check the settings in the Google API console. I received this error on a package mismatch.
Another links of interest are the oauth2 documentation and the google api playground.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With