Created
March 12, 2024 09:55
-
-
Save bjoerntx/3debaac2d80be2826b2db1c6eba0f814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpGet] | |
public string Token() | |
{ | |
string code = Request.Query["code"]; | |
// Security credentials | |
string clientId = ""; | |
string clientSecret = ""; | |
string serviceUrl = "https://trial.dsserver.io"; | |
string authorizationCode = "authorization_code"; | |
BearerToken token; | |
using (HttpClient client = new HttpClient()) | |
{ | |
// Generate the payload | |
var payload = new Dictionary<string, string> | |
{ | |
["grant_type"] = authorizationCode, | |
["code"] = code, | |
["client_id"] = clientId, | |
["client_secret"] = clientSecret, | |
["redirect_uri"] = "https://localhost:7194/Home/Token", | |
}; | |
// Token endpoint | |
string requestUri = $"{serviceUrl}/oauth/token"; | |
// Create the request message | |
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, requestUri) | |
{ | |
Content = new StringContent( | |
UrlEncode(payload), | |
Encoding.UTF8, | |
"application/x-www-form-urlencoded") | |
}; | |
// Send the request | |
var tokenResponse = client.SendAsync(tokenRequest).Result; | |
// Retrieve and return the token | |
var tokenResponseStream = tokenResponse.Content.ReadAsStringAsync().Result; | |
token = JsonConvert.DeserializeObject<BearerToken>(tokenResponseStream); | |
} | |
return token.AccessToken; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment