From e158a40329e84b9ee20d757fa3463a8248e49adf Mon Sep 17 00:00:00 2001 From: daniel furaev <daniel.furaev@stud.hs-hannover.de> Date: Mon, 6 Feb 2023 19:56:03 +0100 Subject: [PATCH] token generation --- lib/services/spotify.dart | 77 +++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/lib/services/spotify.dart b/lib/services/spotify.dart index 1518fd0..e0b9a8b 100644 --- a/lib/services/spotify.dart +++ b/lib/services/spotify.dart @@ -9,8 +9,7 @@ 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 code = "AQCSV8ivXmfyNPrIkq-1xlmsaRmW-zgcrQDKHEQG-axgIFooNsk7tj24-StF_4y3B5ALd4d-efCQM6NkOcyCkQFV4MkuC7kAr8B64Gum8gt5yuutckAFndFDK__HhMQIOyO49OCs7sJIK2e8VHRT5h6dMdCyKUvWg7udxiQSYUHglS97leWWtfo9u_GWK1p3P5Mr_Mg7OYG0DkzY&state=34fFs29kd09"; 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'; @@ -18,42 +17,44 @@ class Spotify { var authToken = 'BQBIbqReZny6ctqIobozxD8dh6w7DBcWLLOYGT3NtngJntXWztPp27C4FZigb55UdjbLnyn_BRw3DlFlwPpgBwU8AeIxXOZvw3fw1liR5zpsyJkVq2ndzXBH0P49p9otYmG9w0RkQy1S_SxVeaZlHHJuImXfL3ckYw5u3Z9iMMtn3i2vnhBJjBjxDHb_WHMEeiRPf14xubjRocD7RvRrJrv0fNNDborP4mdcTS3wUkIOgh-_x--9Y_OKGMOjy9XfN4EcuTFmUVyYPLSYhDB6acRlrK1f'; - generateAuthToken() async { + Future<String> generateAuthToken() async { String encoded = base64Url.encode('$clientId:$clientSecret'.codeUnits); - try { final response = await http.post( - Uri.parse('https://accounts.spotify.com/api/token'), + Uri.parse("https://accounts.spotify.com/api/token"), + headers: {"Content_type": "application/x-www-form-urlencoded"}, body: { - "grant_type": "authorization_code", - 'client_id': "$clientId", - "client_secrete": "$clientSecret", - 'scope': "$scope", - }, - headers: { - 'Authorization': encoded, - }, - ); - final responseJson = json.decode(response.body); + "grant_type": "client_credentials", + "client_id": clientId, + "client_secret": clientSecret + }); + + if (response.statusCode == 200) { + print(200); + Map<String, dynamic> tokenResponse = json.decode(response.body); + authToken = tokenResponse["access_token"]; + print(authToken); + } else if (response.statusCode == 400) { + print(400); + Map<String, dynamic> tokenResponse = json.decode(response.body); + print(tokenResponse["error_description"]); + } else {print("anderer");} - authToken = responseJson['access_token']; - print(authToken); } catch (e) { // TODO: Handle Error } + return authToken; } search(text) async { + var token = generateAuthToken(); 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' - }, + headers: {'Authorization': 'Bearer $authToken'}, ); final responseJson = json.decode(response.body); + print(responseJson); final items = responseJson['tracks']['items'] as List; List songList = items.map((item) => Song.fromJson(item)).toList(); @@ -68,11 +69,9 @@ class Spotify { try { final response = await http.get( Uri.parse('https://api.spotify.com/v1/search?q=genre=$genre'), - headers: { - // 'Authorization': 'Bearer $authToken', - 'Authorization': - 'Bearer BQBIbqReZny6ctqIobozxD8dh6w7DBcWLLOYGT3NtngJntXWztPp27C4FZigb55UdjbLnyn_BRw3DlFlwPpgBwU8AeIxXOZvw3fw1liR5zpsyJkVq2ndzXBH0P49p9otYmG9w0RkQy1S_SxVeaZlHHJuImXfL3ckYw5u3Z9iMMtn3i2vnhBJjBjxDHb_WHMEeiRPf14xubjRocD7RvRrJrv0fNNDborP4mdcTS3wUkIOgh-_x--9Y_OKGMOjy9XfN4EcuTFmUVyYPLSYhDB6acRlrK1f' - }, + headers: {'Authorization': 'Bearer $authToken'}, + + //headers: {HttpHeaders.authorizationHeader: authToken}, ); final responseJson = json.decode(response.body); final items = responseJson['tracks']['items'] as List; @@ -92,7 +91,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/me"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -104,7 +103,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/me/albums"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -116,7 +115,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/me/tracks"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -128,7 +127,7 @@ class Spotify { try { var response = await http.put( Uri.parse("https://api.spotify.com/v1/me/tracks?ids=$track"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -140,7 +139,7 @@ class Spotify { try { var response = await http.delete( Uri.parse("https://api.spotify.com/v1/me/tracks?ids=$track"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -152,7 +151,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/tracks/$id"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -165,7 +164,7 @@ class Spotify { var response = await http.get( Uri.parse( "https://api.spotify.com/v1/recommendations/available-genre-seeds"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -177,7 +176,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/albums/$id"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -189,7 +188,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/albums/$id/tracks"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -201,7 +200,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/artists/$id"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -213,7 +212,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/artists/$id/top-tracks"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { @@ -225,7 +224,7 @@ class Spotify { try { var response = await http.get( Uri.parse("https://api.spotify.com/v1/artists/$id/albums"), - headers: {HttpHeaders.authorizationHeader: authToken}, + headers: {'Authorization': 'Bearer $authToken'}, ); return response; } catch (e) { -- GitLab