2013-01-17 9 views
27

Próbowałem wykonać samouczek: https://developers.google.com/android/guides/http-auth.UserRecoverableAuthException: NeedPermission

Kod:

token = GoogleAuthUtil.getToken(getApplicationContext(), 
         mEmail, mScope); 

manifeście:

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

Błędy:

01-17 18:37:38.230: W/System.err(3689): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission 
01-17 18:37:38.230: W/System.err(3689):  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 
01-17 18:37:38.230: W/System.err(3689):  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 
01-17 18:37:38.230: W/System.err(3689):  at com.example.mgoogleauth.MainActivity$GetIOStreamTask.doInBackground(MainActivity.java:39) 
01-17 18:37:38.230: W/System.err(3689):  at com.example.mgoogleauth.MainActivity$GetIOStreamTask.doInBackground(MainActivity.java:1) 
01-17 18:37:38.230: W/System.err(3689):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
01-17 18:37:38.230: W/System.err(3689):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
01-17 18:37:38.230: W/System.err(3689):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
01-17 18:37:38.230: W/System.err(3689):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
01-17 18:37:38.230: W/System.err(3689):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
01-17 18:37:38.230: W/System.err(3689):  at java.lang.Thread.run(Thread.java:856) 

Odpowiedz

59

Spróbuj po QuickStart Dysk na Androida, jest to przewodnik krok po kroku pokazano, jak w celu autoryzacji i przesłania pliku na Dysk: https://developers.google.com/drive/quickstart-android

Mówiąc dokładniej, wygląda na to, że nie chwyta się wyjątku UserRecoverableException i nie uruchamia zamiaru autoryzacji aplikacji przez użytkownika. Jest to udokumentowane w Google Play docs ty połączone i traktowane w próbie szybkiego startu następująco:

... 
} catch (UserRecoverableAuthIOException e) { 
    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); 
} 
... 
+0

sam problem chociaż skopiowane próbkę jak to jest –

+1

zaktualizowałem odpowiedź z Więcej szczegółów dotyczących sposobu obsługi wyjątku –

+0

REQUEST_AUTHORIZATION powinna być co? – user1400538

12

się getAndUseAuthTokenBlocking() metoda z official GoogleAuthUtil tutorial wyjaśnia całkiem dobrze jak obsłużyć wyjątek:

// Example of how to use the GoogleAuthUtil in a blocking, non-main thread context 
    void getAndUseAuthTokenBlocking() { 
     try { 
      // Retrieve a token for the given account and scope. It will always return either 
      // a non-empty String or throw an exception. 
      final String token = GoogleAuthUtil.getToken(Context, String, String)(context, email, scope); 
      // Do work with token. 
      ... 
      if (server indicates token is invalid) { 
       // invalidate the token that we found is bad so that GoogleAuthUtil won't 
       // return it next time (it may have cached it) 
       GoogleAuthUtil.invalidateToken(Context, String)(context, token); 
       // consider retrying getAndUseTokenBlocking() once more 
       return; 
      } 
      return; 
     } catch (GooglePlayServicesAvailabilityException playEx) { 
     Dialog alert = GooglePlayServicesUtil.getErrorDialog(
      playEx.getConnectionStatusCode(), 
      this, 
      MY_ACTIVITYS_AUTH_REQUEST_CODE); 
     ... 
     } catch (UserRecoverableAuthException userAuthEx) { 
      // Start the user recoverable action using the intent returned by 
      // getIntent() 
      myActivity.startActivityForResult(
        userAuthEx.getIntent(), 
        MY_ACTIVITYS_AUTH_REQUEST_CODE); 
      return; 
     } catch (IOException transientEx) { 
      // network or server error, the call is expected to succeed if you try again later. 
      // Don't attempt to call again immediately - the request is likely to 
      // fail, you'll hit quotas or back-off. 
      ... 
      return; 
     } catch (GoogleAuthException authEx) { 
      // Failure. The call is not expected to ever succeed so it should not be 
      // retried. 
      ... 
      return; 
     } 
    }
6

miałem ten sam błąd, w moim przypadku używałem złego zakresu, po prostu zmienić

https://www.googleapis.com/auth/plus.login 

dla

https://www.googleapis.com/auth/userinfo.profile 
+0

Gdzie muszę zmienić ten @ pablofcn – David

2

Na tej stronie docs https://developers.google.com/+/mobile/android/sign-in przykład ma dobre wyjaśnienie dla tego wyjątku.

W szczególności, wygląda ta linia należy zauważyć:

Żądanie kodu autoryzacyjnego zawsze rzucać UserRecoverableAuthException na pierwsze wezwanie do GoogleAuthUtil.getToken

catch (UserRecoverableAuthException e) { 
    // Requesting an authorization code will always throw 
    // UserRecoverableAuthException on the first call to GoogleAuthUtil.getToken 
    // because the user must consent to offline access to their data. After 
    // consent is granted control is returned to your activity in onActivityResult 
    // and the second call to GoogleAuthUtil.getToken will succeed. 
    startActivityForResult(e.getIntent(), AUTH_CODE_REQUEST_CODE); 
    return; 
} 
1

Docs mają został niedawno zaktualizowany i teraz obsługuje SDK M (żądanie zgody), a także wyświetla okno dialogowe OAuth.

UWAGA Dokumenty Google często nie są aktualne, ale zdają się zwracać uwagę, zgłaszając problem. Przykład został zaktualizowany o tydzień od wysłania opinii. Jeśli widzisz niedziałający przykład, wyślij opinię!

https://developers.google.com/drive/v3/web/quickstart/android