we can actually get a quick connect code now, and authenticating by username and password works!
This commit is contained in:
parent
55e3babc5b
commit
d5017d32ba
96 changed files with 381 additions and 724 deletions
|
@ -7,6 +7,8 @@ using System.Threading.Tasks;
|
|||
using Newtonsoft.Json.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
|
||||
namespace JellyfinRPC
|
||||
{
|
||||
|
@ -177,13 +179,14 @@ namespace JellyfinRPC
|
|||
}
|
||||
return "album_cover";
|
||||
}
|
||||
private static Dictionary<string, string> jsonEntries = new Dictionary<string, string>();
|
||||
public static async Task<string> SendQuickConnectRequest()
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"MediaBrowser Client=\"JellyfinRPC\", Device=\"Windows, Probably\", DeviceId=\"{ConfigManager.GetEntry("JellyfinDeviceID")}\", Version=\"{AssemblyVersion}\"");
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"MediaBrowser Client=\"JellyfinRPC\", Device=\"{Environment.OSVersion}\", DeviceId=\"{ConfigManager.GetEntry("DeviceID")}\", Version=\"{AssemblyVersion}\"");
|
||||
if (ConfigManager.GetEntry("ServerURL") != "")
|
||||
{
|
||||
var response = await httpClient.PostAsync($"{ConfigManager.GetEntry("ServerURL")}/QuickConnect/Initiate", null);
|
||||
var response = await httpClient.GetAsync($"{ConfigManager.GetEntry("ServerURL")}/QuickConnect/Initiate");
|
||||
if (response.StatusCode is (System.Net.HttpStatusCode)401)
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show("This server does not have Quick Connect enabled.", "Quick Connect Unavailable.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1);
|
||||
|
@ -195,26 +198,36 @@ namespace JellyfinRPC
|
|||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
var contents = JArray.Parse(jsonResponse);
|
||||
string[] splitEmUp = jsonResponse.Split(',');
|
||||
|
||||
if (!contents.Any())
|
||||
if (!splitEmUp.Any())
|
||||
{
|
||||
return "Error: Server did not respond.";
|
||||
}
|
||||
|
||||
foreach (var content in contents)
|
||||
else
|
||||
{
|
||||
if (content["Code"]?.ToString() != "")
|
||||
foreach (var splitstring in splitEmUp)
|
||||
{
|
||||
return content["Code"]?.ToString();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Error: Server did not return code.";
|
||||
string[] values = splitstring.Split(':');
|
||||
jsonEntries.Add(values.First().Trim(), String.Join(":", values.Skip(1)).Trim());
|
||||
|
||||
}
|
||||
}
|
||||
return "Error: All values returned are null.";
|
||||
|
||||
|
||||
if (jsonEntries.ContainsKey("\"Code\""))
|
||||
{
|
||||
return jsonEntries["\"Code\""].Replace('"', ' ').Trim();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Error: Server did not return code.";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -241,5 +254,72 @@ namespace JellyfinRPC
|
|||
}
|
||||
|
||||
}
|
||||
private static ConfigForm configForm = new ConfigForm();
|
||||
public static async Task GetTokenFromUsernameAndPassword(string Username, string Password)
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"MediaBrowser Client=\"JellyfinRPC {AssemblyVersion},\", Device=\"{System.Net.Dns.GetHostName()}\", DeviceId=\"{ConfigManager.GetEntry("DeviceID")}\", Version=\"running on {Environment.OSVersion}\"");
|
||||
if (ConfigManager.GetEntry("ServerURL") != "")
|
||||
{
|
||||
|
||||
string username = Username;
|
||||
string password = Password;
|
||||
|
||||
var loginRequest = new Login() { username = username, pw = password };
|
||||
var loginJson = JsonConvert.SerializeObject(loginRequest);
|
||||
|
||||
HttpResponseMessage response = await httpClient.PostAsync($"{ConfigManager.GetEntry("ServerURL")}/Users/AuthenticateByName", new StringContent(loginJson, Encoding.UTF8, "application/json"));
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show("The Username or Password is incorrect.", "Authentication Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var getThatToken = JArray.Parse(jsonResponse);
|
||||
|
||||
foreach (var item in getThatToken)
|
||||
{
|
||||
ConfigManager.SetEntry("JellyfinToken", item["AccessToken"].ToString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Login
|
||||
{
|
||||
public string username { get; set; }
|
||||
public string pw { get; set; }
|
||||
}
|
||||
public class QuickConnect
|
||||
{
|
||||
[JsonProperty("Authenticated")]
|
||||
public bool Authenticated { get; set; }
|
||||
|
||||
[JsonProperty("Secret")]
|
||||
public string Secret { get; set; }
|
||||
|
||||
[JsonProperty("Code")]
|
||||
public string Code { get; set; }
|
||||
|
||||
[JsonProperty("DeviceId")]
|
||||
public string DeviceId { get; set; }
|
||||
|
||||
[JsonProperty("DeviceName")]
|
||||
public string DeviceName { get; set; }
|
||||
|
||||
[JsonProperty("AppName")]
|
||||
public string AppName { get; set; }
|
||||
|
||||
[JsonProperty("AppVersion")]
|
||||
public string AppVersion { get; set; }
|
||||
|
||||
[JsonProperty("DateAdded")]
|
||||
public DateTime DateAdded { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue