UsingOAuth2 is not recommended?

I am working on an Android application that uses the Google Drive API. It was originally created from a quick start example. A simplified sequence of API calls (with proper error handling not shown here):

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 works great, and I'm just about to release my application. But suddenly, after updating the Drive API, I get a warning

The usingOAuth2 (Context, String, String ...) method of type GoogleAccountCredential is deprecated

What's happening? Is there any other way to get credentials? Is there an edited version of quickstart example available anywhere?

Thanks in advance for any clarification. Sean

+8
android google-drive-sdk
source share
2 answers

So, a simple answer to my question:

replace:

GoogleAccountCredential crd = GoogleAccountCredential.usingOAuth2 (this, DriveScopes.DRIVE_FILE);

from

GoogleAccountCredential crd = GoogleAccountCredential.usingOAuth2 (this, Arrays.asList (DriveScopes.DRIVE_FILE));

+28
source share

For completeness

I assume that you are caught in this exception by following the sample code from Google Drive QuickStart . If so, you can find the following things that I had to change.

Google Play Services Lib (see comment below!)

The documentation uses the old way to add libraries to an Android project. This will fail when executed using the latest ADT. You will be able to compile and download to the device / emulator, but upon execution you will receive a NoClassDefFoundError.

 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/AccountPicker; 

To fix this, you must copy the google-play-services.jar file to the libs folder.

Missing meta tag

Until the next error. Then I got an IllegalStateException with instructions on adding a meta tag in the manifest with google-play-services version.

So, in the manifest application tag, 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, lib corresponded to this version. If you enter the wrong version here, you will receive an error message indicating the correct version. So be sure to check out.

Permission denied

Finally, I received an invitation for oauth in the application to find out that the sample application still does not have permission. Error for reference:

 java.io.FileNotFoundException: /storage/emulated/0/Pictures/IMG_20131211_110629.jpg: open failed: EACCES (Permission denied) 

Next to the rights listed in the example:

 <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.INTERNET" /> 

Also add permission WRITE_EXTERNAL_STORAGE to the manifest:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Additional resources

If you get a com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException exception and the Unknown root description is somewhere, you should check the settings in the Google API console . I got this error when the package does not match.

Other interesting links are the oauth2 documentation and google api playground .

+3
source share

All Articles