OAuth2GetXOAuthKeyStatic Method
The version of GetXOAuthKey(String, String) which does not require you to create an instance of OAuth2 class.

Namespace: MailBee
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
Syntax
public static string GetXOAuthKeyStatic(
	string email,
	string accessToken
)

Parameters

email
Type: SystemString
E-mail address, like user@domain.com.
accessToken
Type: SystemString
Access token.

Return Value

Type: String
A string with URI for XOAUTH2 authentication process for IMAP/SMTP.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptionemail or accessToken is a null reference (Nothing in Visual Basic) or an empty string.
Remarks
When using Google API Auth Client library, the only required method of OAuth2 class is GetXOAuthKeyStatic(String, String), you don't need to use other methods of this class. For that purpose, this static (Shared in Visual Basic) version exists.
Examples

Two samples in this topic. You can find more samples and explanations in OAuth 2.0 in Windows and ASP.NET MVC apps guide.

The first sample performs OAUTH2 authentication using Google Service accounts (common method for Google Apps). You'll need to reference Google API Auth Client library (with NuGet). Note that at least .NET 4.0 is required for this library.

using System;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using MailBee;
using MailBee.ImapMail;

public class Program
{
    public static void Main(string[] args)
    {
        String serviceAccountEmail = "88...-...7j@developer.gserviceaccount.com";
        string userEmail = "user@some-googleapps-domain.com";

        var certificate = new X509Certificate2(@"C:\Temp\a...0-privatekey.p12",
            "secret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            User = userEmail,
            Scopes = new[] { "https://mail.google.com/" }
        }.FromCertificate(certificate));

        if (credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
        {
            string imapXOAuthKey = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken);

            Imap imp = new Imap();

            // Logging is not necessary but useful for debugging.
            imp.Log.Enabled = true;
            imp.Log.Filename = @"C:\Temp\log.txt";
            imp.Log.Clear();

            imp.Connect("imap.gmail.com");
            imp.Login(userEmail, imapXOAuthKey, AuthenticationMethods.SaslOAuth2,
                AuthenticationOptions.None, null);
            imp.SelectFolder("Inbox");
            imp.Disconnect();
        }
    }
}
The second sample connects to a regular Google account. In its IMAP part, it's similar to the sample in OAuth2 topic but uses Google API Auth Client library instead of DotNetOpenAuth.dll. The key advantage is that no user input is required (copy-paste from the browser to the console). Also, it shows OAuth2 usage with SMTP, not just IMAP.
using System;
using Google.Apis.Auth.OAuth2;
using MailBee;
using MailBee.ImapMail;
using MailBee.SmtpMail;

public class Program
{
    public static void Main(string[] args)
    {
        string userEmail = "john.doe@gmail.com";

        UserCredential credential;
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "somewhat like 628470012345.apps.googleusercontent.com",
                ClientSecret = "somewhat like 8IbRSofadFQ7ThjClxp8GBa2"
            },
            new[] { "https://mail.google.com/" },
            "user",
            System.Threading.CancellationToken.None).Result;

        string xoauthKey = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken);

        bool useImap = true; // Set to false to use SMTP instead of IMAP.

        if (useImap)
        {
            Imap imp = new Imap();

            // Logging is not necessary but useful for debugging.
            imp.Log.Enabled = true;
            imp.Log.Filename = @"C:\Temp\log.txt";
            imp.Log.Clear();

            imp.Connect("imap.gmail.com");
            imp.Login(userEmail, xoauthKey, AuthenticationMethods.SaslOAuth2,
                AuthenticationOptions.None, null);
            imp.SelectFolder("Inbox");
            imp.Disconnect();
            Console.WriteLine("SUCCEEDED");
        }
        else
        {
            Smtp mailer = new Smtp();
            mailer.SmtpServers.Add("smtp.gmail.com", userEmail, xoauthKey, AuthenticationMethods.SaslOAuth2);

            // Logging is not necessary but useful for debugging.
            mailer.Log.Enabled = true;
            mailer.Log.Filename = @"C:\Temp\log.txt";
            mailer.Log.Clear();

            mailer.From.Email = userEmail;
            mailer.To.Add(userEmail);
            mailer.Subject = "empty email to myself";

            mailer.Send();
        }
    }
}
See Also