Initial commit
This commit is contained in:
52
Jugenddienst Stunden/Models/Auth.cs
Normal file
52
Jugenddienst Stunden/Models/Auth.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jugenddienst_Stunden.Models
|
||||
{
|
||||
|
||||
class Auth
|
||||
{
|
||||
|
||||
public Hours hours;
|
||||
|
||||
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
|
||||
using (HttpClient client = new HttpClient()) {
|
||||
try {
|
||||
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
// Senden der Anfrage und Abrufen der Antwort
|
||||
HttpResponseMessage response = await client.GetAsync(url);
|
||||
|
||||
// Überprüfen, ob die Anfrage erfolgreich war
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
// Lesen und Rückgabe der Antwort als String
|
||||
string responseData = await response.Content.ReadAsStringAsync();
|
||||
return responseData;
|
||||
} catch (HttpRequestException e) {
|
||||
// Fehlerbehandlung
|
||||
Console.WriteLine($"An error occurred: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task Main(string[] args) {
|
||||
string apiUrl = args[0]; // Ersetzen Sie dies durch die tatsächliche URL der API
|
||||
string bearerToken = args[1]; // Ersetzen Sie hier durch Ihr tatsächliches Token
|
||||
|
||||
string data = await GetApiDataWithAuthAsync(apiUrl, bearerToken);
|
||||
|
||||
if (data != null) {
|
||||
//Console.WriteLine("API-Daten:");
|
||||
//Console.WriteLine(data);
|
||||
//JOperator operator = Newtonsoft.Json.JsonConvert.DeserializeObject<JOperator>(data);
|
||||
//var zeug = Newtonsoft.Json.JsonConvert.DeserializeObject(data);
|
||||
Hours hours = Newtonsoft.Json.JsonConvert.DeserializeObject<Hours>(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user