Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

usingOAuth2 deprecated?

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

like image 881
seanpj Avatar asked Jul 02 '13 15:07

seanpj


2 Answers

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));

like image 110
seanpj Avatar answered Nov 08 '22 05:11

seanpj


For completeness

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.

Google Play Services Lib (NB. See comment below!)

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.

Missing meta-tag

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.

Permission Denied

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"/>

More resources

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.

like image 25
hcpl Avatar answered Nov 08 '22 06:11

hcpl