Search code examples
flutterauthorizationspotify

Handling query parameters in authorization callback on flutter


I'm trying to use Spotify Web API with Flutter. I'm requesting for a user login from Spotify with openAuthorizationUrl() but it's returning a web page and I don't know how to handle it. I have to use this "code" parameter but I'm not even in the app. Here is the function:

void openAuthorizationUrl() async {

final clientId = 'cd3acd...7efbc';
final redirectUri = 'https://localhost:8888/callback';
final scope = 'user-read-private user-read-email user-top-read';
final response_type = 'code';

final authorizationUrl =
    'https://accounts.spotify.com/authorize?response_type=$response_type&client_id=$clientId&scope=$scope&redirect_uri=$redirectUri';

if (await canLaunchUrl(Uri.parse(authorizationUrl))) {
  var x = await launchUrl(Uri.parse(authorizationUrl));
} else {
  throw 'Could not launch $authorizationUrl';
 }
}

Here is the screenshot of aplication: enter image description here


Solution

  • I changed url scheme http to my app's name like "com.example.myapp://localhost:8888/callback/" and I used flutter_web_auth2 package. http scheme is not redirecting to application all the time, so.