Skip to content
Snippets Groups Projects
Select Git revision
  • 73017c94f8fa6f3b2c101f3988784969fc416637
  • main default protected
  • music_player
  • WirMachenFirebaseMitRein
  • madlen
  • mergeHELL
  • ede
  • spotify-api
8 results

spotify.dart

Blame
  • spotify.dart 7.33 KiB
    import 'dart:convert';
    import 'dart:developer';
    import 'dart:io';
    
    import 'package:flutter/services.dart';
    import 'package:http/http.dart' as http;
    import 'package:spotify_sdk/spotify_sdk.dart';
    
    class Spotify {
      final clientId = '60cf1944b15d4f9fb223ba32ce6336d1';
      final clientSecret = 'd2a2fd28d1c14d14babd781b8d5b52f5';
      final email = 'ambient.spotify@gmail.com';
      final password = "Start123\$";
    
      final scope =
          'user-library-modify playlist-modify-private playlist-read-private user-follow-read user-read-current-playing user-library-read user-read-private user-top-read user-follow-modify user-read-recent-played';
    
      var authToken =
          'BQBIbqReZny6ctqIobozxD8dh6w7DBcWLLOYGT3NtngJntXWztPp27C4FZigb55UdjbLnyn_BRw3DlFlwPpgBwU8AeIxXOZvw3fw1liR5zpsyJkVq2ndzXBH0P49p9otYmG9w0RkQy1S_SxVeaZlHHJuImXfL3ckYw5u3Z9iMMtn3i2vnhBJjBjxDHb_WHMEeiRPf14xubjRocD7RvRrJrv0fNNDborP4mdcTS3wUkIOgh-_x--9Y_OKGMOjy9XfN4EcuTFmUVyYPLSYhDB6acRlrK1f';
    
      generateAuthToken() async {
        String encoded = base64Url.encode('$clientId:$clientSecret'.codeUnits);
    
        try {
          final response = await http.post(
            Uri.parse('https://accounts.spotify.com/api/token'),
            body: {
              'grant_type': 'password',
              'username': email,
              'password': password,
              'scope': scope,
            },
            headers: {
              'Authorization': encoded,
            },
          );
          final responseJson = json.decode(response.body);
    
          authToken = responseJson['access_token'];
        } catch (e) {
          // TODO: Handle Error
        }
      }
    
      search(text) async {
        try {
          final response = await http.get(
            Uri.parse('https://api.spotify.com/v1/search?q=$text&type=track'),
            headers: {
              // 'Authorization': 'Bearer $authToken',
              'Authorization':
                  'Bearer BQBIbqReZny6ctqIobozxD8dh6w7DBcWLLOYGT3NtngJntXWztPp27C4FZigb55UdjbLnyn_BRw3DlFlwPpgBwU8AeIxXOZvw3fw1liR5zpsyJkVq2ndzXBH0P49p9otYmG9w0RkQy1S_SxVeaZlHHJuImXfL3ckYw5u3Z9iMMtn3i2vnhBJjBjxDHb_WHMEeiRPf14xubjRocD7RvRrJrv0fNNDborP4mdcTS3wUkIOgh-_x--9Y_OKGMOjy9XfN4EcuTFmUVyYPLSYhDB6acRlrK1f'
            },
          );
          final responseJson = json.decode(response.body);
          final items = responseJson['tracks']['items'] as List;
    
          List songList = items.map((item) => Song.fromJson(item)).toList();
    
          return songList;
        } catch (e) {
          log(e.toString());
        }
      }
    
      playGenre(genre) async {
        try {
          final response = await http.get(
            Uri.parse('https://api.spotify.com/v1/search?q=genre=$genre'),
            headers: {