Flutter Spotify Api Authentication

Viewed 3165

I need to authenticate user in my Flutter app which uses Spotify api. I use flutter_web_auth to open WebView and let user login there. I can't make it to return to app.

In Spotify Dashboard i have callback Uri set to: https://spotifydata.com/callback

      final callbackUrl = "https://spotifydata.com/callback";

      void _authenticateSpotfy() async {
        final url = Uri.https('accounts.spotify.com', '/authorize', {
        'response_type': 'code',
        'client_id': clientID,
        'redirect_uri': 'https://spotifydata.com/callback:/',
        'scope': 'user-read-private user-read-email',
    });


    final result = await FlutterWebAuth.authenticate(
        url: url.toString(), callbackUrlScheme: callbackUrl);
  }

AndroidManifest.xml

<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
    <intent-filter android:label="flutter_web_auth">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https://spotifydata.com/callback" />
    </intent-filter>
</activity>

I have tried other answers on Stack but they don't seem to work.

1 Answers

As mentioned in Spotify's API docs, authentication redirects the user to the defined redirect_uri on successful request. Trying to log-in on https://spotifydata.com/, it has redirect_uri set as redirect_uri=https%3A%2F%2Fspotifydata.com%2Fsongdata. Make sure that the defined redirect_uri matches the callback scheme configured in the Manifest file since this enables the app to handle the redirect URL via the intent-filter.

Related