7

I followed all steps listen under https://developers.google.com/drive/quickstart-android It works! But if im going to merge the given code/package to an other project, im getting

 W/System.err(793): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
 W/System.err(793): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
 W/System.err(793): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
 W/System.err(793): at com.drive.main.MainActivity$GetToken.doInBackground(MainActivity.java:139)
 W/System.err(793): at com.drive.main.MainActivity$GetToken.doInBackground(MainActivity.java:1)
 W/System.err(793): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 W/System.err(793): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 W/System.err(793): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 W/System.err(793): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 W/System.err(793): at java.lang.Thread.run(Thread.java:856)
 I/System.out(793): An error occurred: com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException

Am I doing something wrong in the Manifest or should i change something in the Api Console?

Found only one thread under stackoverflow without any answer Google Drive Authentication Exception - Needs Permission? (v2)

Any ideas would be helpful. Thank you.

Community
  • 1
  • 1
benjamin ali
  • 146
  • 1
  • 12

1 Answers1

23

You should catch the UserRecoverableAuthException and in the catch block, you recover the Intent from the exception by calling UserRecoverableAuthException#getIntent(). Start that intent to take user to the OAuth2 permission page for your app.

Sample code -

try {
    Drive service = Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
        .build();
    // Do whatever you want with the Drive service
} catch (UserRecoverableAuthIOException e) {
    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • 1
    Do you know how to repeat the UserRecoverableAuthIOException after you grant access? I need to test and debug my entire auth process – Mike6679 May 07 '13 at 01:55
  • Well, that's painful. I used to uninstall the app and restart the device to get that working. – jaibatrik May 07 '13 at 03:46
  • How can I avoid this exception in case I had previously got permission using GD Android API call? `connectionResult.startResolutionForResult(activity, COMPLETE_AUTHORIZATION_REQUEST_CODE)` – Konstantin Konopko Sep 04 '14 at 13:49
  • I am working with the Gmail API. I did this, got the window to allow Gmail API to send mails. But I get a 400 bad request on that. Any idea on that anyone ? – Dinesh Oct 25 '16 at 23:46
  • @Mike6679 try to remove permissions manually here: https://myaccount.google.com/permissions – Vít Kapitola Oct 07 '21 at 13:20