mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2025-01-11 14:07:47 +00:00
Added support for .net 4.6
This commit is contained in:
parent
f97fd83cca
commit
2f8bf6877f
@ -7,6 +7,7 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
{
|
{
|
||||||
public static void OpenBrowser(string url)
|
public static void OpenBrowser(string url)
|
||||||
{
|
{
|
||||||
|
#if NETSTANDARD2_0
|
||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
{
|
{
|
||||||
url = url.Replace("&", "^&");
|
url = url.Replace("&", "^&");
|
||||||
@ -24,6 +25,10 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
{
|
{
|
||||||
// throw
|
// throw
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
url = url.Replace("&", "^&");
|
||||||
|
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using SpotifyAPI.Web.Enums;
|
using SpotifyAPI.Web.Enums;
|
||||||
@ -12,6 +9,13 @@ using SpotifyAPI.Web.Models;
|
|||||||
using Unosquare.Labs.EmbedIO;
|
using Unosquare.Labs.EmbedIO;
|
||||||
using Unosquare.Labs.EmbedIO.Constants;
|
using Unosquare.Labs.EmbedIO.Constants;
|
||||||
using Unosquare.Labs.EmbedIO.Modules;
|
using Unosquare.Labs.EmbedIO.Modules;
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
using System.Net.Http;
|
||||||
|
#endif
|
||||||
|
#if NET46
|
||||||
|
using System.Net.Http;
|
||||||
|
using HttpListenerContext = Unosquare.Net.HttpListenerContext;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace SpotifyAPI.Web.Auth
|
namespace SpotifyAPI.Web.Auth
|
||||||
{
|
{
|
||||||
@ -91,7 +95,11 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
Error = error
|
Error = error
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
return context.StringResponseAsync("OK - This window can be closed now");
|
return context.StringResponseAsync("OK - This window can be closed now");
|
||||||
|
#else
|
||||||
|
return context.StringResponseAsync("OK - This window can be closed now");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
[WebApiHandler(HttpVerbs.Post, "/")]
|
[WebApiHandler(HttpVerbs.Post, "/")]
|
||||||
@ -107,9 +115,7 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
auth.SecretId = (string) formParams["secretId"];
|
auth.SecretId = (string) formParams["secretId"];
|
||||||
|
|
||||||
string uri = auth.GetUri();
|
string uri = auth.GetUri();
|
||||||
context.Response.Redirect(uri);
|
return context.Redirect(uri, false);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System.Net;
|
using System;
|
||||||
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using SpotifyAPI.Web.Enums;
|
using SpotifyAPI.Web.Enums;
|
||||||
using SpotifyAPI.Web.Models;
|
using SpotifyAPI.Web.Models;
|
||||||
@ -18,20 +19,22 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
|
|
||||||
protected override WebServer AdaptWebServer(WebServer webServer)
|
protected override WebServer AdaptWebServer(WebServer webServer)
|
||||||
{
|
{
|
||||||
return webServer.WithWebApiController<ImplictGrantAuthController>();
|
Console.WriteLine("Hello");
|
||||||
|
webServer.Module<WebApiModule>().RegisterController<ImplictGrantAuthController>();
|
||||||
|
return webServer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImplictGrantAuthController : WebApiController
|
public class ImplictGrantAuthController : WebApiController
|
||||||
{
|
{
|
||||||
[WebApiHandler(HttpVerbs.Get, "/auth")]
|
[WebApiHandler(HttpVerbs.Get, "/authe")]
|
||||||
public Task<bool> GetAuth(WebServer server, HttpListenerContext context)
|
public bool GetAuth(WebServer server, HttpListenerContext context)
|
||||||
{
|
{
|
||||||
string state = context.Request.QueryString["state"];
|
string state = context.Request.QueryString["state"];
|
||||||
SpotifyAuthServer<Token> auth = ImplictGrantAuth.GetByState(state);
|
SpotifyAuthServer<Token> auth = ImplictGrantAuth.GetByState(state);
|
||||||
if (auth == null)
|
if (auth == null)
|
||||||
return context.StringResponseAsync(
|
return true; /*context.StringResponseAsync(
|
||||||
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");
|
$"Failed - Unable to find auth request with state \"{state}\" - Please retry");*/
|
||||||
|
|
||||||
Token token;
|
Token token;
|
||||||
string error = context.Request.QueryString["error"];
|
string error = context.Request.QueryString["error"];
|
||||||
@ -56,7 +59,8 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
}
|
}
|
||||||
|
|
||||||
Task.Factory.StartNew(() => auth?.TriggerAuth(token));
|
Task.Factory.StartNew(() => auth?.TriggerAuth(token));
|
||||||
return context.StringResponseAsync("OK - This window can be closed now");
|
return true;
|
||||||
|
// return context.StringResponseAsync("OK - This window can be closed now");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
@ -15,7 +15,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="EmbedIO" Version="1.15.0" />
|
<PackageReference Include="EmbedIO" Version="1.16.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -30,4 +30,9 @@
|
|||||||
<Folder Include="Properties\" />
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
|
||||||
|
<Reference Include="System.Net" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -23,7 +23,9 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
</metadata>
|
</metadata>
|
||||||
<files>
|
<files>
|
||||||
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.dll" target="lib\SpotifyAPI.Web.Auth.dll" />
|
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.dll" target="lib\netstandard2.0\SpotifyAPI.Web.Auth.dll" />
|
||||||
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.xml" target="lib\SpotifyAPI.Web.Auth.xml" />
|
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.Auth.xml" target="lib\netstandard2.0\SpotifyAPI.Web.Auth.xml" />
|
||||||
|
<file src="bin\Release\net46\SpotifyAPI.Web.Auth.dll" target="lib\net46\SpotifyAPI.Web.Auth.dll" />
|
||||||
|
<file src="bin\Release\net46\SpotifyAPI.Web.Auth.xml" target="lib\net46\SpotifyAPI.Web.Auth.xml" />
|
||||||
</files>
|
</files>
|
||||||
</package>
|
</package>
|
||||||
|
@ -5,6 +5,7 @@ using System.Text;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using SpotifyAPI.Web.Enums;
|
using SpotifyAPI.Web.Enums;
|
||||||
using Unosquare.Labs.EmbedIO;
|
using Unosquare.Labs.EmbedIO;
|
||||||
|
using Unosquare.Labs.EmbedIO.Constants;
|
||||||
using Unosquare.Labs.EmbedIO.Modules;
|
using Unosquare.Labs.EmbedIO.Modules;
|
||||||
|
|
||||||
namespace SpotifyAPI.Web.Auth
|
namespace SpotifyAPI.Web.Auth
|
||||||
@ -20,6 +21,7 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
|
|
||||||
private readonly string _folder;
|
private readonly string _folder;
|
||||||
private readonly string _type;
|
private readonly string _type;
|
||||||
|
private WebServer _server;
|
||||||
protected CancellationTokenSource _serverSource;
|
protected CancellationTokenSource _serverSource;
|
||||||
|
|
||||||
public delegate void OnAuthReceived(object sender, T payload);
|
public delegate void OnAuthReceived(object sender, T payload);
|
||||||
@ -41,10 +43,13 @@ namespace SpotifyAPI.Web.Auth
|
|||||||
{
|
{
|
||||||
Instances.Add(State, this);
|
Instances.Add(State, this);
|
||||||
_serverSource = new CancellationTokenSource();
|
_serverSource = new CancellationTokenSource();
|
||||||
WebServer server = AdaptWebServer(WebServer.Create(ServerUri));
|
|
||||||
server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
|
_server = WebServer.Create(ServerUri, RoutingStrategy.Regex);
|
||||||
|
_server.RegisterModule(new WebApiModule());
|
||||||
|
AdaptWebServer(_server);
|
||||||
|
_server.RegisterModule(new ResourceFilesModule(typeof(ImplictGrantAuth).Assembly, $"SpotifyAPI.Web.Auth.Resources.{_folder}"));
|
||||||
#pragma warning disable 4014
|
#pragma warning disable 4014
|
||||||
server.RunAsync(_serverSource.Token);
|
_server.RunAsync(_serverSource.Token);
|
||||||
#pragma warning restore 4014
|
#pragma warning restore 4014
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,14 +27,19 @@ namespace SpotifyAPI.Web.Example
|
|||||||
"Tip: If you want to supply your ClientID and SecretId beforehand, use env variables (SPOTIFY_CLIENT_ID and SPOTIFY_SECRET_ID)");
|
"Tip: If you want to supply your ClientID and SecretId beforehand, use env variables (SPOTIFY_CLIENT_ID and SPOTIFY_SECRET_ID)");
|
||||||
|
|
||||||
|
|
||||||
AuthorizationCodeAuth auth =
|
AuthorizationCodeAuth auth =
|
||||||
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
|
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
|
||||||
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
|
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
|
||||||
auth.AuthReceived += AuthOnAuthReceived;
|
auth.AuthReceived += AuthOnAuthReceived;
|
||||||
|
auth.Start();
|
||||||
|
auth.OpenBrowser();
|
||||||
|
|
||||||
|
/* ImplictGrantAuth auth = new ImplictGrantAuth("26d287105e31491889f3cd293d85bfea", "http://localhost:4002", "http://localhost:4002");
|
||||||
auth.Start();
|
auth.Start();
|
||||||
auth.OpenBrowser();
|
auth.OpenBrowser();*/
|
||||||
|
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
auth.Stop(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async void AuthOnAuthReceived(object sender, AuthorizationCode payload)
|
private static async void AuthOnAuthReceived(object sender, AuthorizationCode payload)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
@ -18,4 +18,9 @@
|
|||||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
|
||||||
|
<Reference Include="System.Net" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -22,7 +22,9 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
</metadata>
|
</metadata>
|
||||||
<files>
|
<files>
|
||||||
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.dll" target="lib\SpotifyAPI.Web.dll" />
|
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.dll" target="lib\netstandard2.0\SpotifyAPI.Web.dll" />
|
||||||
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.xml" target="lib\SpotifyAPI.Web.xml" />
|
<file src="bin\Release\netstandard2.0\SpotifyAPI.Web.xml" target="lib\netstandard2.0\SpotifyAPI.Web.xml" />
|
||||||
|
<file src="bin\Release\net46\SpotifyAPI.Web.dll" target="lib\net46\SpotifyAPI.Web.dll" />
|
||||||
|
<file src="bin\Release\net46\SpotifyAPI.Web.xml" target="lib\net46\SpotifyAPI.Web.xml" />
|
||||||
</files>
|
</files>
|
||||||
</package>
|
</package>
|
||||||
|
Loading…
Reference in New Issue
Block a user