11using System ;
22using System . Collections . Generic ;
33using System . Net ;
4+ using System . Net . Http ;
45using System . Text ;
56using System . Threading . Tasks ;
67using Newtonsoft . Json ;
910using Unosquare . Labs . EmbedIO ;
1011using Unosquare . Labs . EmbedIO . Constants ;
1112using Unosquare . Labs . EmbedIO . Modules ;
12- #if NETSTANDARD2_0
13- using System . Net . Http ;
14- #endif
13+ using Unosquare . Swan ;
1514#if NET46
16- using System . Net . Http ;
1715using HttpListenerContext = Unosquare . Net . HttpListenerContext ;
1816#endif
1917
@@ -45,12 +43,32 @@ public override string GetUri()
4543 return ShouldRegisterNewApp ( ) ? $ "{ RedirectUri } /start.html#{ State } " : base . GetUri ( ) ;
4644 }
4745
48- protected override WebServer AdaptWebServer ( WebServer webServer ) => webServer . WithWebApiController < AuthorizationCodeAuthController > ( ) ;
46+ protected override WebServer AdaptWebServer ( WebServer webServer )
47+ {
48+ return webServer . WithWebApiController < AuthorizationCodeAuthController > ( ) ;
49+ }
4950
50- public async Task < Token > ExchangeCode ( string code )
51+ private string GetAuthHeader ( ) => $ "Basic { Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( ClientId + ":" + SecretId ) ) } ";
52+
53+ public async Task < Token > RefreshToken ( string refreshToken )
5154 {
52- string auth = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( ClientId + ":" + SecretId ) ) ;
55+ List < KeyValuePair < string , string > > args = new List < KeyValuePair < string , string > > ( )
56+ {
57+ new KeyValuePair < string , string > ( "grant_type" , "refresh_token" ) ,
58+ new KeyValuePair < string , string > ( "refresh_token" , refreshToken )
59+ } ;
5360
61+ HttpClient client = new HttpClient ( ) ;
62+ client . DefaultRequestHeaders . Add ( "Authorization" , GetAuthHeader ( ) ) ;
63+ HttpContent content = new FormUrlEncodedContent ( args ) ;
64+
65+ HttpResponseMessage resp = await client . PostAsync ( "https://accounts.spotify.com/api/token" , content ) ;
66+ string msg = await resp . Content . ReadAsStringAsync ( ) ;
67+
68+ return JsonConvert . DeserializeObject < Token > ( msg ) ;
69+ }
70+ public async Task < Token > ExchangeCode ( string code )
71+ {
5472 List < KeyValuePair < string , string > > args = new List < KeyValuePair < string , string > > ( )
5573 {
5674 new KeyValuePair < string , string > ( "grant_type" , "authorization_code" ) ,
@@ -59,7 +77,7 @@ public async Task<Token> ExchangeCode(string code)
5977 } ;
6078
6179 HttpClient client = new HttpClient ( ) ;
62- client . DefaultRequestHeaders . Add ( "Authorization" , $ "Basic { auth } " ) ;
80+ client . DefaultRequestHeaders . Add ( "Authorization" , GetAuthHeader ( ) ) ;
6381 HttpContent content = new FormUrlEncodedContent ( args ) ;
6482
6583 HttpResponseMessage resp = await client . PostAsync ( "https://accounts.spotify.com/api/token" , content ) ;
0 commit comments