googleapis_auth icon indicating copy to clipboard operation
googleapis_auth copied to clipboard

Failed assertion when doing clientViaUserConsent

Open floitschG opened this issue 7 years ago • 7 comments

From @floitsch on December 2, 2017 16:23

When doing a clientViaUserConsent I get the following assertion error:

When running without assertions, the code seems to work.

Unhandled exception:
'package:googleapis_auth/src/auth_http_utils.dart': Failed assertion: line 81 pos 12: 'credentials.refreshToken != null': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/dart:core/errors_patch.dart:37)
#1      _AssertionError._throwNew (dart:core-patch/dart:core/errors_patch.dart:33)
#2      new AutoRefreshingClient (package:googleapis_auth/src/auth_http_utils.dart:81:12)
#3      clientViaUserConsent.<anonymous closure> (package:googleapis_auth/auth_io.dart:53:32)
#4      _RootZone.runUnary (dart:async/zone.dart:1381)
#5      _FutureListener.handleValue (dart:async/future_impl.dart:129)
#6      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633)
#7      _Future._propagateToListeners (dart:async/future_impl.dart:662)
#8      _Future._complete (dart:async/future_impl.dart:467)
#9      _SyncCompleter.complete (dart:async/future_impl.dart:51)
#10     _completeOnAsyncReturn (dart:async-patch/dart:async/async_patch.dart:255)
#11     AuthorizationCodeGrantServerFlow.run (package:googleapis_auth/src/oauth2_flows/auth_code.dart:223:9)
#12     _RootZone.runUnary (dart:async/zone.dart:1381)
#13     _FutureListener.handleValue (dart:async/future_impl.dart:129)
#14     _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633)
#15     _Future._propagateToListeners (dart:async/future_impl.dart:662)
#16     _Future._addListener.<anonymous closure> (dart:async/future_impl.dart:342)
#17     _microtaskLoop (dart:async/schedule_microtask.dart:41)
#18     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#19     _Timer._runTimers (dart:isolate-patch/dart:isolate/timer_impl.dart:376)
#20     _Timer._handleMessage (dart:isolate-patch/dart:isolate/timer_impl.dart:401)
#21     _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:163)

main() {
  clientViaUserConsent(new ClientId(_CLIENT_ID, _CLIENT_SECRET), _SCOPES, (String str) {
    print("asking for consent: $str");
  }).then((client) {
  });

Copied from original issue: dart-lang/googleapis#26

floitschG avatar Dec 06 '17 17:12 floitschG

From @mkustermann on December 4, 2017 11:58

This issue should probably moved to package:googleapis_auth.

The assertion is correct in the sense that the oauth2 access token you got back will most likely only be valid for 1h and expire afterwards. Getting a refresh token from the server allows the returned AutoRefreshingAuthClient to refresh the access token before it expires. That's why the assertion is in.

The real question is why the server hasn't returned the refresh token in the first place.

floitschG avatar Dec 06 '17 17:12 floitschG

I tried removing the permission from my account, but that wasn't enough. https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token

Some comments claim that one has to set the access_type to offline. Given the assert, we should probably always set "access-type" to "offline" all the time.

floitschG avatar Dec 09 '17 16:12 floitschG

Could you describe how you created the client Id in the developers console?

mkustermann avatar Dec 10 '17 21:12 mkustermann

I had created the my credentials for "webapp" and not "other". Not sure, if we still want to do something.

floitschG avatar Dec 12 '17 20:12 floitschG

I think we can turn the assertion into a check and an exception. Although we can never know why the server did not return a refresh token, we could hint the user that the provided client credentials were wrong.

mkustermann avatar Dec 13 '17 20:12 mkustermann

Sounds good to me, although I wonder if there should be a way to use web-tokens for scripts too.

floitschG avatar Dec 14 '17 14:12 floitschG

I add parameter to fix this problem #79

GuillermoDLCO avatar Jul 16 '20 00:07 GuillermoDLCO