Implémentation refresh token et clean up du code

messagerie_lucas_test
Félix MIELCAREK 2 years ago
parent 3d1ff99fb5
commit e5875c5dc6

@ -3,73 +3,110 @@ import 'dart:math';
import 'package:http/http.dart' as http;
class Api {
var clientId = '7ceb49d874b9404492246027e4d68cf8';
var clientSecret = '98f9cb960bf54ebbb9ad306e7ff919cb';
var redirectUri = 'https://daflmusic.000webhostapp.com/callback/';
var state;
String scopes = 'user-read-playback-state user-read-currently-playing';
var code;
var urlAuthorize;
String? access_token;
String token_type = 'Bearer ';
int? expires_in;
String? refresh_token;
//from dashboard
final _clientId = '7ceb49d874b9404492246027e4d68cf8';
final _clientSecret = '98f9cb960bf54ebbb9ad306e7ff919cb'; // TODO : hide it
var client = http.Client();
//for web api
get redirectUri => 'https://daflmusic.000webhostapp.com/callback/';
final _scopes = 'user-read-playback-state user-read-currently-playing';
String? _state;
String? _encodedLogs;
final _tokenType = 'Bearer ';
//from web api
String? _code;
int? _expiresIn;
String? _refreshToken;
String? _accessToken; //use _getToken() as kind of a private getter
//other
final _client = http.Client();
Uri? _urlAuthorize;
get urlAuthorize => _urlAuthorize;
DateTime? _tokenEnd;
Api() {
state = generateRandomString();
urlAuthorize = Uri.https('accounts.spotify.com', 'authorize', {
'client_id': clientId,
_state = _generateRandomString();
_encodedLogs = base64.encode(utf8.encode("$_clientId:$_clientSecret"));
_urlAuthorize = Uri.https('accounts.spotify.com', 'authorize', {
'client_id': _clientId,
'response_type': 'code',
'redirect_uri': redirectUri,
'state': state,
'scope': scopes,
'state': _state,
'scope': _scopes,
'show_dialog': 'true'
});
}
requestUserAuthorization(Uri url) {
if (url.queryParameters['state'] == state.toString()) {
code = url.queryParameters['code'];
requestAccessToken();
// for state value
String _generateRandomString() {
var r = Random();
return String.fromCharCodes(
List.generate(16, (index) => r.nextInt(33) + 89));
}
//session management
requestUserAuthorization(Uri url) async {
if (url.queryParameters['state'] == _state.toString()) {
_code = url.queryParameters['code'];
await _requestAccessToken();
}
// TODO : implement the else
}
requestAccessToken() async {
_requestAccessToken() async {
var urlToken = Uri.https('accounts.spotify.com', 'api/token', {
'code': code,
'code': _code,
'redirect_uri': redirectUri,
'grant_type': 'authorization_code'
});
String credentials = "$clientId:$clientSecret";
String encodedLogs = base64.encode(utf8.encode(credentials));
var response = await client.post(urlToken, headers: <String, String>{
'Authorization': 'Basic $encodedLogs',
var response = await _client.post(urlToken, headers: <String, String>{
'Authorization': 'Basic $_encodedLogs',
'Content-Type': 'application/x-www-form-urlencoded'
});
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes)) as Map;
_accessToken = decodedResponse['access_token'];
_expiresIn = decodedResponse['expires_in'];
_tokenEnd = DateTime.now().add(Duration(seconds: _expiresIn!));
_refreshToken = decodedResponse['refresh_token'];
}
Future<String?> _getToken() async {
await _tokenValidity();
return _accessToken;
}
_tokenValidity() async {
if (DateTime.now().isAfter(_tokenEnd!)) {
await _getRefreshedAccessToken();
}
}
_getRefreshedAccessToken() async {
var urlToken = Uri.https('accounts.spotify.com', 'api/token',
{'grant_type': 'refresh_token', 'refresh_token': '$_refreshToken'});
var response = await _client.post(urlToken, headers: <String, String>{
'Authorization': 'Basic $_encodedLogs',
'Content-Type': 'application/x-www-form-urlencoded'
});
var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes)) as Map;
access_token = decodedResponse['access_token'];
expires_in = decodedResponse['expires_in'];
refresh_token = decodedResponse['refresh_token'];
getCurrentlyPlayingTrack();
_accessToken = decodedResponse['access_token'];
_expiresIn = decodedResponse['expires_in'];
_tokenEnd = DateTime.now().add(Duration(seconds: _expiresIn!));
}
//functional methods
getCurrentlyPlayingTrack() async {
var url = Uri.https('api.spotify.com', 'v1/me/player/currently-playing');
var response = await client.get(url, headers: <String, String>{
'Authorization': '$token_type $access_token',
var token = await _getToken();
var response = await _client.get(url, headers: <String, String>{
'Authorization': '$_tokenType $token',
'Content-Type': 'application/json'
});
// Implement traitement of datas
print(response.body);
}
// for state value
String generateRandomString() {
var r = Random();
return String.fromCharCodes(
List.generate(16, (index) => r.nextInt(33) + 89));
// Implement treatment of data's
//print(response.body);
}
}

@ -24,7 +24,8 @@ class MyInAppBrowser extends InAppBrowser {
@override
Future onLoadStart(url) async {
if (url!.origin + url.path == MyApp.api.redirectUri) {
MyApp.api.requestUserAuthorization(url);
await MyApp.api.requestUserAuthorization(url);
await MyApp.api.getCurrentlyPlayingTrack();
close();
}
}

@ -0,0 +1,11 @@
//
// Generated file. Do not edit.
//
// clang-format off
#include "generated_plugin_registrant.h"
void fl_register_plugins(FlPluginRegistry* registry) {
}

@ -0,0 +1,15 @@
//
// Generated file. Do not edit.
//
// clang-format off
#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_
#include <flutter_linux/flutter_linux.h>
// Registers Flutter plugins.
void fl_register_plugins(FlPluginRegistry* registry);
#endif // GENERATED_PLUGIN_REGISTRANT_

@ -0,0 +1,23 @@
#
# Generated file, do not edit.
#
list(APPEND FLUTTER_PLUGIN_LIST
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST
)
set(PLUGIN_BUNDLED_LIBRARIES)
foreach(plugin ${FLUTTER_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin})
target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin)
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
endforeach(plugin)
foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/linux plugins/${ffi_plugin})
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
endforeach(ffi_plugin)
Loading…
Cancel
Save