Skip to content
Snippets Groups Projects
Commit 1bfb3dba authored by Klaus Fischer's avatar Klaus Fischer
Browse files

feat: Get OAuth token working from code

parent 8557986b
No related branches found
No related tags found
1 merge request!3Feature/440 mvp net sdk part 1
...@@ -15,4 +15,8 @@ ...@@ -15,4 +15,8 @@
<PackageReference Include="coverlet.collector" Version="3.1.0" /> <PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FitConnect\FitConnect.csproj" />
</ItemGroup>
</Project> </Project>
using System; using System;
using System.IO; using System.IO;
using FitConnect;
using FluentAssertions; using FluentAssertions;
using Newtonsoft.Json; using Newtonsoft.Json;
using NUnit.Framework; using NUnit.Framework;
...@@ -9,6 +10,7 @@ namespace E2ETests; ...@@ -9,6 +10,7 @@ namespace E2ETests;
public class SenderTest { public class SenderTest {
private string _clientId = ""; private string _clientId = "";
private string _clientSecret = ""; private string _clientSecret = "";
private Sender _sender;
[OneTimeSetUp] [OneTimeSetUp]
public void OneTimeSetup() { public void OneTimeSetup() {
...@@ -37,6 +39,8 @@ public class SenderTest { ...@@ -37,6 +39,8 @@ public class SenderTest {
[SetUp] [SetUp]
public void Setup() { public void Setup() {
_sender = new Sender(null,
FitConnectEndpoints.Create(FitConnectEndpoints.EndpointType.Development));
} }
[Test] [Test]
...@@ -44,4 +48,11 @@ public class SenderTest { ...@@ -44,4 +48,11 @@ public class SenderTest {
_clientId.Should().NotBe("00000000-0000-0000-0000-000000000000"); _clientId.Should().NotBe("00000000-0000-0000-0000-000000000000");
_clientSecret.Should().NotBe("0000000000000000000000000000000000000000000"); _clientSecret.Should().NotBe("0000000000000000000000000000000000000000000");
} }
[Test]
public void GetAccessToken() {
var token = _sender.GetTokenAsync(_clientId, _clientSecret).Result;
token.Should().NotBeNull();
token.ExpiresIn.Should().Be(1800);
}
} }
using System.Net.Http.Headers;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text; using System.Text;
using FitConnect.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace FitConnect; namespace FitConnect;
...@@ -85,6 +87,26 @@ public class FunctionalBaseClass { ...@@ -85,6 +87,26 @@ public class FunctionalBaseClass {
} }
public async Task<OAuthAccessToken?> GetTokenAsync(string clientId, string clientSecret) {
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
MediaTypeWithQualityHeaderValue.Parse("application/json"));
var content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret)
});
var request = new HttpRequestMessage(HttpMethod.Post, Endpoints.TokenUrl) {
Content = content,
Method = HttpMethod.Post
};
var response = await client.SendAsync(request);
return await response.Content.ReadFromJsonAsync<OAuthAccessToken>();
}
private async Task<T?> RestCall<T>(Uri uri, HttpMethod method, string body) { private async Task<T?> RestCall<T>(Uri uri, HttpMethod method, string body) {
var client = new HttpClient(); var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Add("Accept", "application/json");
......
using System.Text.Json.Serialization;
namespace FitConnect.Models;
public class OAuthAccessToken {
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }
[JsonPropertyName("scope")]
public string Scope { get; set; }
[JsonPropertyName("token_type")]
public string TokenType { get; set; }
[JsonPropertyName("expires_in")]
public int ExpiresIn { get; set; }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment