Last active
December 28, 2022 14:25
-
-
Save kasuken/ef57f1e54e8b0e8092590ae7984da197 to your computer and use it in GitHub Desktop.
Auth0 Device Flow - Console application .NET 7
This file contains hidden or 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
using Newtonsoft.Json.Linq; | |
using RestSharp; | |
const string tenant = "dev-n1b7bzid.us"; | |
const string clientId = "wnvXuZ1rD5VeT4NGBzr1MDNLWu43H5KA"; | |
string access_token = string.Empty; | |
var client = new RestClient($"https://{tenant}.auth0.com/oauth/device/code"); | |
var request = new RestRequest(); | |
request.Method = Method.Post; | |
request.AddHeader("content-type", "application/x-www-form-urlencoded"); | |
request.AddParameter("application/x-www-form-urlencoded", $"client_id={clientId}&scope=offline_access+openid+profile", ParameterType.RequestBody); | |
var response = client.Execute(request); | |
var json = JObject.Parse(response.Content); | |
var devicecode = json["device_code"].Value<string>(); | |
var user_code = json["user_code"].Value<string>(); | |
var verification_uri = json["verification_uri"].Value<string>(); | |
var interval = json["interval"].Value<int>(); | |
var expires = json["expires_in"].Value<int>(); | |
Console.WriteLine($"Open a browser and paste the url: {verification_uri}, then insert the code: {user_code}"); | |
Console.WriteLine("--------------------------------------------"); | |
for (int i = 0; i < (expires / interval); i++) | |
{ | |
Task.Delay(interval * 1000).Wait(); | |
client = new RestClient($"https://{tenant}.auth0.com/oauth/token"); | |
request = new RestRequest(); | |
request.Method = Method.Post; | |
request.AddHeader("content-type", "application/x-www-form-urlencoded"); | |
request.AddParameter("application/x-www-form-urlencoded", $"grant_type=urn:ietf:params:oauth:grant-type:device_code&device_code={devicecode}&client_id={clientId}", ParameterType.RequestBody); | |
response = client.Execute(request); | |
json = JObject.Parse(response.Content); | |
if (response.IsSuccessStatusCode) | |
{ | |
access_token = json["access_token"].Value<string>(); | |
Console.WriteLine($"Your token is: {access_token}"); | |
break; | |
} | |
else | |
{ | |
var error = json["error"].Value<string>(); | |
var error_description = json["error_description"].Value<string>(); | |
Console.WriteLine($"Status: {error} - {error_description}"); | |
} | |
} | |
//Call your API with the token | |
client = new RestClient("https://fakerestapi.azurewebsites.net/api/v1/Activities"); | |
request = new RestRequest(); | |
request.Method= Method.Get; | |
request.AddHeader("content-type", "appliScation/json"); | |
request.AddHeader("authorization", $"Bearer {access_token}"); | |
response = client.Execute(request); | |
Console.WriteLine("--------------------------------------------"); | |
Console.WriteLine("Response from API:"); | |
Console.WriteLine(response.Content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment