teranetsrl / oauth2_client

Simple Dart library for interacting with OAuth2 servers.
BSD 2-Clause "Simplified" License
95 stars 113 forks source link

Token is not getting saved in storage. #27

Closed hvkalayil closed 4 years ago

hvkalayil commented 4 years ago

My code Implementation as per the given documentation

I tried to use the package to get data from Spotify API. I made a new client as shown below

class SpotifyOAuth2Client extends OAuth2Client {
  SpotifyOAuth2Client(
      {@required String redirectUri, @required String customUriScheme})
      : super(
            authorizeUrl:
                'https://accounts.spotify.com/authorize', 
            tokenUrl:
                'https://accounts.spotify.com/api/token', 
            redirectUri: redirectUri,
            customUriScheme: customUriScheme) {
    this.accessTokenRequestHeaders = {'Accept': 'application/json'};
  }
}

I used this code to get data using the helper

    SpotifyOAuth2Client client = SpotifyOAuth2Client(
        customUriScheme: 'mypakagename',
        redirectUri: 'mypackagename:/oauth2redirect');

    OAuth2Helper helper = OAuth2Helper(client,
        grantType: OAuth2Helper.AUTHORIZATION_CODE, clientId: _keys[0]);

The Problem

This code works perfectly, except this wont store tokens Everytime this code is run, it redirects me to get an accesstoken again. So I tried the code below.

My improvisation

OAuth2Helper auth2helper = OAuth2Helper(client);
    auth2helper.setAuthorizationParams(
        grantType: OAuth2Helper.AUTHORIZATION_CODE,
        clientId: _keys[0],
        clientSecret: _keys[1]);
    auth2helper.getToken();

Error found during the improvisation When I execute this code I get the following error

Unhandled Exception: Exception: "state" parameter in response doesn't correspond to the expected value

Is there something wrong with the package or is my implementationwrong? Do help me out

Complete Error trace

This is the complete error trace. PS This code is in the file _spotifyclient.dart in the class SpotifyAPI

E/flutter (30049): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Exception: "state" parameter in response doesn't correspond to the expected value E/flutter (30049): #0 new AuthorizationResponse.fromRedirectUri (package:oauth2_client/authorization_response.dart:31:9) E/flutter (30049): #1 OAuth2Client.requestAuthorization (package:oauth2_client/oauth2_client.dart:148:34) E/flutter (30049): E/flutter (30049): #2 OAuth2Client.getTokenWithAuthCodeFlow (package:oauth2_client/oauth2_client.dart:77:26) E/flutter (30049): #3 OAuth2Helper.fetchToken (package:oauth2_client/oauth2_helper.dart:100:30) E/flutter (30049): #4 OAuth2Helper.getToken (package:oauth2_client/oauth2_helper.dart:78:23) E/flutter (30049): E/flutter (30049): #5 OAuth2Helper.get (package:oauth2_client/oauth2_helper.dart:212:25) E/flutter (30049): #6 SpotifyAPI.getData (package:musicplayer/utilities/spotify_client.dart:51:43) E/flutter (30049): #7 _rootRunUnary (dart:async/zone.dart:1198:47) E/flutter (30049): #8 _CustomZone.runUnary (dart:async/zone.dart:1100:19) E/flutter (30049): #9 _FutureListener.handleValue (dart:async/future_impl.dart:143:18) E/flutter (30049): #10 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45) E/flutter (30049): #11 Future._propagateToListeners (dart:async/future_impl.dart:725:32) E/flutter (30049): #12 Future._completeWithValue (dart:async/future_impl.dart:529:5) E/flutter (30049): #13 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:40:15) E/flutter (30049): #14 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:311:13) E/flutter (30049): #15 kGetKeys (package:musicplayer/utilities/constants.dart) E/flutter (30049): #16 _rootRunUnary (dart:async/zone.dart:1198:47) E/flutter (30049): #17 _CustomZone.runUnary (dart:async/zone.dart:1100:19) E/flutter (30049): #18 _FutureListener.handleValue (dart:async/future_impl.dart:143:18) E/flutter (30049): #19 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45) E/flutter (30049): #20 Future._propagateToListeners (dart:async/future_impl.dart:725:32) E/flutter (30049): #21 Future._completeWithValue (dart:async/future_impl.dart:529:5) E/flutter (30049): #22 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:40:15) E/flutter (30049): #23 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:311:13) E/flutter (30049): #24 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart) E/flutter (30049): #25 _rootRunUnary (dart:async/zone.dart:1198:47) E/flutter (30049): #26 _CustomZone.runUnary (dart:async/zone.dart:1100:19) E/flutter (30049): #27 _FutureListener.handleValue (dart:async/future_impl.dart:143:18) E/flutter (30049): #28 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45) E/flutter (30049): #29 Future._propagateToListeners (dart:async/future_impl.dart:725:32) E/flutter (30049): #30 Future._completeWithValue (dart:async/future_impl.dart:529:5) E/flutter (30049): #31 _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:40:15) E/flutter (30049): #32 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:311:13) E/flutter (30049): #33 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart) E/flutter (30049): #34 _rootRunUnary (dart:async/zone.dart:1198:47) E/flutter (30049): #35 _CustomZone.runUnary (dart:async/zone.dart:1100:19) E/flutter (30049): #36 _FutureListener.handleValue (dart:async/future_impl.dart:143:18) E/flutter (30049): #37 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45) E/flutter (30049): #38 Future._propagateToListeners (dart:async/future_impl.dart:725:32) E/flutter (30049): #39 Future._completeWithValue (dart:async/future_impl.dart:529:5) E/flutter (30049): #40 Future._asyncCompleteWithValue. (dart:async/future_impl.dart:567:7) E/flutter (30049): #41 _rootRun (dart:async/zone.dart:1190:13) E/flutter (30049): #42 _CustomZone.run (dart:async/zone.dart:1093:19) E/flutter (30049): #43 _CustomZone.runGuarded (dart:async/zone.dart:997:7) E/flutter (30049): #44 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23) E/flutter (30049): #45 _microtaskLoop (dart:async/schedule_microtask.dart:41:21) E/flutter (30049): #46 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

okrad commented 4 years ago

Hi @hvkalayil, thank you for the detailed report. There was a problem when empty scopes (or no scopes at all) were used. It should be fixed now, can you check the latest release (1.4.3)?

hvkalayil commented 4 years ago

Thanks. Its working now. This package is extremely useful. Thanks for your work

benjaminhorner commented 2 years ago

I am getting this on version 2.2.4…

Sewx commented 2 years ago

I am experiencing this in version 2.3.2. For android its working perfectly but in iOS it keeps trying to redirect me to get an accesstoken again.

Mubaola23 commented 2 years ago

I am experiencing this in version 2.3.2. For both android and ios android here is the full error detail

W/libEGL (27460): EGLNativeWindowType 0xba667008 disconnect failed D/SurfaceView(27460): remove() io.flutter.embedding.android.FlutterSurfaceView{c59a186 V.E...... ........ 0,0-720,1494} Surface(name=SurfaceView - ng.myapp.com/ng.myapp.com.MainActivity@c59a186@0)/@0xd05d18a W/libEGL (27460): EGLNativeWindowType 0xe7196e48 disconnect failed D/ViewRootImpl@1a68380MainActivity: Relayout returned: old=(0,0,720,1520) new=(0,0,720,1520) req=(720,1520)8 dur=19 res=0x5 s={false 0} ch=true D/ViewRootImpl@1a68380MainActivity: stopped(true) old=false D/SurfaceView(27460): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{c59a186 V.E...... ........ 0,0-720,1494} of ViewRootImpl@1a68380[MainActivity] W/ActivityThread(27460): handleWindowVisibility: no activity for token android.os.BinderProxy@8a00c18 D/PhoneWindow(27460): forceLight changed to true [] from com.android.internal.policy.PhoneWindow.updateForceLightNavigationBar:4238 com.android.internal.policy.DecorView.updateColorViews:1494 com.android.internal.policy.PhoneWindow.dispatchWindowAttributesChanged:3216 android.view.Window.setFlags:1148 com.android.internal.policy.PhoneWindow.generateLayout:2444 I/MultiWindowDecorSupport(27460): updateCaptionType >> DecorView@24c38ad[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true D/MultiWindowDecorSupport(27460): setCaptionType = 0, DecorView = DecorView@24c38ad[] D/ViewRootImpl@1a68380MainActivity: stopped(false) old=true D/SurfaceView(27460): windowStopped(false) false io.flutter.embedding.android.FlutterSurfaceView{c59a186 V.E...... ........ 0,0-720,1494} of ViewRootImpl@1a68380[MainActivity] D/ViewRootImpl@1a68380MainActivity: stopped(false) old=false D/SurfaceView(27460): onWindowVisibilityChanged(0) true io.flutter.embedding.android.FlutterSurfaceView{c59a186 V.E...... ........ 0,0-720,1494} of ViewRootImpl@1a68380[MainActivity] E/flutter (27460): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: "state" parameter in response doesn't correspond to the expected value E/flutter (27460): #0 new AuthorizationResponse.fromRedirectUri (package:oauth2_client/authorization_response.dart:32:11) E/flutter (27460): #1 OAuth2Client.requestAuthorization (package:oauth2_client/oauth2_client.dart:233:34) E/flutter (27460): E/flutter (27460): #2 OAuth2Client.getTokenWithAuthCodeFlow (package:oauth2_client/oauth2_client.dart:149:20) E/flutter (27460): E/flutter (27460): #3 OAuth2Helper.fetchToken (package:oauth2_client/oauth2_helper.dart:126:17) E/flutter (27460): E/flutter (27460): #4 OAuth2Helper.getToken (package:oauth2_client/oauth2_helper.dart:99:17) E/flutter (27460): E/flutter (27460): #5 MyHelper.authorize (package:myApp/src/services/network/oAuth2_client.dart:42:7) E/flutter (27460): E/flutter (27460): #6 LoginNotifier.login (package:onDgo/src/feature/authentication/notifier/login_notifier.dart:26:7) E/flutter (27460):

durga-nannamsetty commented 1 year ago

Hi @Mubaola23. I am facing the same issue. Did you found any fix? Note: It is happening only in some android devices.