OAuth2Bridge is a simple and powerful C# library that simplifies OAuth2 authentication with Discord. This library handles user authentication via Discord’s OAuth2 flow, making it easy to authenticate users and retrieve their data.
- Discord OAuth2 Authentication: Allows your application to authenticate users using Discord’s OAuth2 service.
- Customizable Scopes: Easily configure the required Discord OAuth2 scopes for your application (e.g.,
Identify,Email, etc.). - User Info Retrieval: Retrieve user information after authentication, including their username, email, avatar, and more.
- Easy Integration: Simple API designed for ease of use with minimal configuration.
To get started, you can install the OAuth2Bridge library via NuGet:
Install-Package OAuth2Bridge
To authenticate a user, you just need to create an instance of OAuthServer, configure your OAuth credentials, and call the AuthenticateAsync method.
using OAuth2Bridge;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
class Program
{
static void Main(string[] args)
{
try
{
// Run the async Auth method
Auth().Wait();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private static async Task Auth()
{
var logger = LoggerFactory.Create(builder => builder.AddConsole())
.CreateLogger<OAuthLogger>();
var oAuthLogger = new OAuthLogger(logger);
// Create the OAuth server instance
var server = OAuthServer.CreateServer("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET", 3465, oAuthLogger, "Your App Name");
// Add necessary Discord scopes
server.Scopes.Add(DiscordScopes.Email);
server.Scopes.Add(DiscordScopes.Identify);
try
{
// Start the authentication process
var userInfo = await server.AuthenticateAsync(CancellationToken.None, @".\data\success.html");
Console.WriteLine(JsonConvert.SerializeObject(userInfo, Formatting.Indented));
}
catch (OAuthException ex)
{
Console.WriteLine($"Authentication failed: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
}
}You can customize the OAuth2 scopes to request specific permissions from the user. Some commonly used scopes include:
Identify- Access the user's username and avatar.Email- Access the user's email address.Guilds- Get the list of guilds the user is a member of.
Here’s how you can add scopes:
server.Scopes.Add(DiscordScopes.Guilds);
server.Scopes.Add(DiscordScopes.Email);Once authenticated, you can easily retrieve the authenticated user’s information:
public class UserInfo
{
public string Id { get; set; }
public string Username { get; set; }
public string Avatar { get; set; }
public string Email { get; set; }
}You can use JsonConvert.SerializeObject to display the user's info in a readable format:
Console.WriteLine(JsonConvert.SerializeObject(userInfo, Formatting.Indented));You will need to configure the following details to authenticate with Discord’s OAuth2 service:
ClientId: Your Discord application's client ID.ClientSecret: Your Discord application's client secret.
var server = OAuthServer.CreateServer("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET", 3465, oAuthLogger, "Your App Name");OAuth2Bridge is licensed under the MIT License. See the LICENSE file for details.
- Removed references to timeout handling from the "Features" section.
- Adjusted the examples and explanations to align with the removal of timeout functionality.
- Simplified the
AuthenticateAsyncusage, as it now waits indefinitely for the callback.

