admin管理员组

文章数量:1320627

I am trying to make an application with Spotify api, but when there is a successful login, the redirectUri redirects to my application and I get a CANCELED, user has canceled the login warning in my application.

My codes are below, thank you in advance.

class ApiService {
  static const clientId = '//';
  static const clientSecret = '//';
  static const redirectUri = 'menapps://callback';

  static const String authUrl = '';
  static const String tokenUrl = '';
  static const String userProfileUrl = '';

  static Future<String?> authenticate() async {
    final authUrlWithParams =
        '$authUrl?response_type=code&client_id=$clientId&scope=user-read-private user-read-email&redirect_uri=$redirectUri';

    final result = await FlutterWebAuth.authenticate(
        url: authUrlWithParams, callbackUrlScheme: 'menapps');

    final code = Uri.parse(result).queryParameters['code'];

    try {
      final result = await FlutterWebAuth.authenticate(
          url: authUrlWithParams, callbackUrlScheme: 'menapps');

      final code = Uri.parse(result).queryParameters['code'];

      if (code != null) {
        return await _exchangeCodeForToken(code);
      } else {
         return null;
      }
    } catch (e) {
      if (e.toString().contains('CANCELLED')) {
         print('Kullanıcı giriş işlemini iptal etti.');
        return null;
      } else {
        print('Bir hata oluştu: $e');
        return null;
      }
    }
  }


  static Future<String?> _exchangeCodeForToken(String code) async {
    try {
      final dio = Dio();
      final response = await dio.post(
        tokenUrl,
        data: {
          'grant_type': 'authorization_code',
          'code': code,
          'redirect_uri': redirectUri,
        },
        options: Options(
          headers: {
            'Authorization': 'Basic ' + base64Encode(utf8.encode('$clientId:$clientSecret')),
          },
        ),
      );

      if (response.statusCode == 200) {
        return response.data['access_token'];
      } else {
        print('Token alma başarısız oldu: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      print('Bir hata oluştu: $e');
      return null;
    }
  }

  static Future<Map<String, dynamic>?> getUserProfile(String token) async {
///
}

After logging in, I want to be transferred to HomePage and show people's licenses.

本文标签: mobileFlutter Spotify API return CANCELLEDStack Overflow