Skip to content

Commit e7f4047

Browse files
author
Jonas Dellinger
committed
Added method to RefreshToken for auth code flow
Fixed some includes
1 parent a302694 commit e7f4047

3 files changed

Lines changed: 33 additions & 19 deletions

File tree

‎SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net;
4+
using System.Net.Http;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Newtonsoft.Json;
@@ -9,11 +10,8 @@
910
using Unosquare.Labs.EmbedIO;
1011
using Unosquare.Labs.EmbedIO.Constants;
1112
using 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;
1715
using 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);

‎SpotifyAPI.Web.Auth/SpotifyAuthServer.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void Start()
4747
_server = WebServer.Create(ServerUri, RoutingStrategy.Regex);
4848
_server.RegisterModule(new WebApiModule());
4949
AdaptWebServer(_server);
50-
_server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
50+
_server.RegisterModule(new ResourceFilesModule(typeof(T).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
5151
#pragma warning disable 4014
5252
_server.RunAsync(_serverSource.Token);
5353
#pragma warning restore 4014

‎SpotifyAPI.Web.Example/Program.cs‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
namespace SpotifyAPI.Web.Example
88
{
9-
static class Program
9+
internal static class Program
1010
{
1111
private static string _clientId = ""; //"";
1212
private static string _secretId = ""; //"";
1313

1414
static void Main(string[] args)
1515
{
1616
_clientId = string.IsNullOrEmpty(_clientId)
17-
? System.Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID")
17+
? Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID")
1818
: _clientId;
1919

2020
_secretId = string.IsNullOrEmpty(_secretId)
21-
? System.Environment.GetEnvironmentVariable("SPOTIFY_SECRET_ID")
21+
? Environment.GetEnvironmentVariable("SPOTIFY_SECRET_ID")
2222
: _secretId;
2323

2424
Console.WriteLine("####### Spotify API Example #######");
@@ -34,10 +34,6 @@ static void Main(string[] args)
3434
auth.Start();
3535
auth.OpenBrowser();
3636

37-
/* ImplictGrantAuth auth = new ImplictGrantAuth("26d287105e31491889f3cd293d85bfea", "http://localhost:4002", "http://localhost:4002");
38-
auth.Start();
39-
auth.OpenBrowser();*/
40-
4137
Console.ReadLine();
4238
auth.Stop(0);
4339
}
@@ -48,15 +44,15 @@ private static async void AuthOnAuthReceived(object sender, AuthorizationCode pa
4844
auth.Stop();
4945

5046
Token token = await auth.ExchangeCode(payload.Code);
51-
SpotifyWebAPI api = new SpotifyWebAPI()
47+
SpotifyWebAPI api = new SpotifyWebAPI
5248
{
5349
AccessToken = token.AccessToken,
5450
TokenType = token.TokenType
5551
};
56-
PrintUsefulDate(api);
52+
PrintUsefulData(api);
5753
}
5854

59-
private static async void PrintUsefulDate(SpotifyWebAPI api)
55+
private static async void PrintUsefulData(SpotifyWebAPI api)
6056
{
6157
PrivateProfile profile = await api.GetPrivateProfileAsync();
6258
string name = string.IsNullOrEmpty(profile.DisplayName) ? profile.Id : profile.DisplayName;

0 commit comments

Comments
 (0)