Android Drive API: getting Sys.err UserRecoverable

2019-01-26 14:12发布

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.

1条回答
太酷不给撩
2楼-- · 2019-01-26 14:41

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);
}
查看更多
登录 后发表回答