MailBee.NET Objects 7.2

OAuth Class

This class provides methods for OAuth authentication process.

For a list of all members of this type, see OAuth Members.

System.Object
   MailBee.OAuth

public class OAuth

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

The OAuth protocol enables websites or applications to access Protected Resources from a web service via an API without requiring Users to disclose their Service Provider credentials to these web sites or applications. It means that a 3d party application doesn't need to get the username and the password to access data via API from the restricted application.

For instance, your application can ask the user to authorize its attempt to access data in your Gmail mailbox. MailBee provides you with some URL on Google's web site you need to open in the browser. The page asks the user if he/she confirms that your application is trusted to access your mailbox. If the user confirms that, Google returns the key the user needs to supply to your application (and to MailBee in the end). MailBee transforms this key, and you can then use it with OAuth method when logging in Gmail account of the user.

The methods of this class allow you to pass the OAuth authorization process and get OAuth keys which can be used for authentication on IMAP/SMTP servers that support XOAUTH extension. You can also use this class to get OAuth keys for other purposes with any software capable of OAuth authentication because the produced keys are not specific to MailBee.

Examples section contains several samples of using 2-legged and 3-legged OAuth with GMail IMAP and SMTP.

Example

This sample authenticates client in Gmail IMAP via 2-legged OAuth. At this time Gmail doesn't allow any user to sign up for 2-legged OAuth support. But this may change in the future.

[C#]
using System;
using System.Collections.Specialized;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    static void Main(string[] args)
    {
        string consumerKey = "";
        string consumerSecret = "";
        string userEmail = "e-mail@gmail.com";

        OAuth myOAuth = new OAuth(consumerKey, consumerSecret);

        StringDictionary parameters = new StringDictionary();
        parameters.Add("xoauth_requestor_id", userEmail);

        // Get XOAuth key for IMAP.
        string imapXOAuthKey = myOAuth.GetXOAuthKey(
            string.Format("https://mail.google.com/mail/b/{0}/imap/", userEmail));

        // Last: Login to Gmail IMAP server using XOAuth.
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("imap.gmail.com", 993);
        imp.Login(null, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, null);
        imp.SelectFolder("INBOX");

        // Processing of INBOX emails here.
        //...
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports System.Collections.Specialized
Imports MailBee
Imports MailBee.ImapMail

Class Sample
    Shared Sub Main(ByVal args() As String)
        Dim consumerKey As String = ""
        Dim consumerSecret As String = ""
        Dim userEmail As String = "e-mail@gmail.com"

        Dim myOAuth As OAuth = New OAuth(consumerKey, consumerSecret)

        Dim parameters As StringDictionary = New StringDictionary
        parameters.Add("xoauth_requestor_id", userEmail)

        ' Get XOAuth key for IMAP.
        Dim imapXOAuthKey As String = myOAuth.GetXOAuthKey( _
            String.Format("https://mail.google.com/mail/b/{0}/imap/", userEmail))

        ' Last: Login to Gmail IMAP server using XOAuth.
        Dim imp As Imap = New Imap

        ' Connect to the server, login and select inbox.
        imp.Connect("imap.gmail.com", 993)
        imp.Login(Nothing, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, Nothing)
        imp.SelectFolder("INBOX")

        ' Processing of INBOX emails here.
        '...
        imp.Disconnect()
    End Sub
End Class

This sample authenticates client in Gmail IMAP via 3-legged OAuth with "anonymous" consumer key pairs. User may get registered his/her site for an e-mail account and specify keys from Google "Registration for Web Applications" page.
[C#]
using System;
using System.Collections.Specialized;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    static void Main(string[] args)
    {
        Console.Write("Please enter your email: ");
        string userEmail = Console.ReadLine().Trim();

        OAuth myOAuth = new OAuth("anonymous", "anonymous");

        StringDictionary parameters = new StringDictionary();
        parameters.Add("scope", "https://mail.google.com/");
        parameters.Add(OAuth.OAuthCallbackKey, "oob");

        // First: Fetch Request Token
        myOAuth.RequestToken("https://www.google.com/accounts/OAuthGetRequestToken", parameters);

        // Second: Redirect to Authorization and Fire up the browser
        System.Diagnostics.Process.Start(
            myOAuth.AuthorizeToken("https://www.google.com/accounts/OAuthAuthorizeToken"));

        Console.Write("Please enter the key: ");
        string key = Console.ReadLine().Trim();

        // Third: Fetch Access Token
        myOAuth.AccessToken("https://www.google.com/accounts/OAuthGetAccessToken", key);

        // Get XOAuth key for IMAP.
        string imapXOAuthKey = myOAuth.GetXOAuthKey(
            string.Format("https://mail.google.com/mail/b/{0}/imap/", userEmail));

        // Last: Login to Gmail IMAP server using XOAuth.
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("imap.gmail.com", 993);
        imp.Login(null, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, null);
        imp.SelectFolder("INBOX");

        // Processing of INBOX emails here.
        //...
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports System.Collections.Specialized
Imports MailBee
Imports MailBee.ImapMail

Class Sample
    Shared Sub Main(ByVal args() As String)
        Console.Write("Please enter your email: ")
        Dim userEmail As String = Console.ReadLine().Trim()

        Dim myOAuth As OAuth = New OAuth("anonymous", "anonymous")

        Dim parameters As StringDictionary = New StringDictionary
        parameters.Add("scope", "https://mail.google.com/")
        parameters.Add(OAuth.OAuthCallbackKey, "oob")

        ' First: Fetch Request Token
        myOAuth.RequestToken("https://www.google.com/accounts/OAuthGetRequestToken", parameters)

        ' Second: Redirect to Authorization and Fire up the browser
        System.Diagnostics.Process.Start( _
            myOAuth.AuthorizeToken("https://www.google.com/accounts/OAuthAuthorizeToken"))

        Console.Write("Please enter the key: ")
        Dim key As String = Console.ReadLine().Trim()

        ' Third: Fetch Access Token
        myOAuth.AccessToken("https://www.google.com/accounts/OAuthGetAccessToken", key)

        ' Get XOAuth key for IMAP.
        Dim imapXOAuthKey As String = myOAuth.GetXOAuthKey( _
            String.Format("https://mail.google.com/mail/b/{0}/imap/", userEmail))

        ' Last: Login to Gmail IMAP server using XOAuth.
        Dim imp As Imap = New Imap

        ' Connect to the server, login and select inbox.
        imp.Connect("imap.gmail.com", 993)
        imp.Login(Nothing, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, Nothing)
        imp.SelectFolder("INBOX")

        ' Processing of INBOX emails here.
        '...
        imp.Disconnect()
    End Sub
End Class

This sample authenticates client in Gmail SMTP via 3-legged OAuth with "anonymous" consumer key pairs and sends e-mail message.
[C#]
using System;
using System.Collections.Specialized;
using MailBee;
using MailBee.SmtpMail;

class Sample
{
    static void Main(string[] args)
    {
        Console.Write("Please enter your email: ");
        string userEmail = Console.ReadLine().Trim();

        OAuth myOAuth = new OAuth("anonymous", "anonymous");

        StringDictionary parameters = new StringDictionary();
        parameters.Add("scope", "https://mail.google.com/");
        parameters.Add(OAuth.OAuthCallbackKey, "oob");

        // First: Fetch Request Token
        myOAuth.RequestToken("https://www.google.com/accounts/OAuthGetRequestToken", parameters);

        // Second: Redirect to Authorization and Fire up the browser
        System.Diagnostics.Process.Start(
            myOAuth.AuthorizeToken("https://www.google.com/accounts/OAuthAuthorizeToken"));

        Console.Write("Please enter the key: ");
        string key = Console.ReadLine().Trim();

        // Third: Fetch Access Token
        myOAuth.AccessToken("https://www.google.com/accounts/OAuthGetAccessToken", key);

        // Get XOAuth key for IMAP.
        string smtpXOAuthKey = myOAuth.GetXOAuthKey(
            string.Format("https://mail.google.com/mail/b/{0}/smtp/", userEmail));

        // Last: Login to Gmail SMTP server using XOAuth.

        // Connect to the server, login and send e-mail.
        SmtpServer server = new SmtpServer("smtp.gmail.com", 465, 0);
        server.AuthMethods = AuthenticationMethods.SaslOAuth;
        server.AccountName = null;
        server.Password = smtpXOAuthKey;

        Smtp mailer = new Smtp();

        mailer.From.AsString = userEmail;
        mailer.To.Add("e-mail@host.com");
        mailer.Subject = "My subject";
        mailer.BodyPlainText = "Text body";

        mailer.SmtpServers.Add(server);
        mailer.Send();
    }
}
[Visual Basic]
Imports System
Imports System.Collections.Specialized
Imports MailBee
Imports MailBee.SmtpMail

Class Sample
    Shared Sub Main(ByVal args() As String)
        Console.Write("Please enter your email: ")
        Dim userEmail As String = Console.ReadLine().Trim()

        Dim myOAuth As OAuth = New OAuth("anonymous", "anonymous")

        Dim parameters As StringDictionary = New StringDictionary
        parameters.Add("scope", "https://mail.google.com/")
        parameters.Add(OAuth.OAuthCallbackKey, "oob")

        ' First: Fetch Request Token
        myOAuth.RequestToken("https://www.google.com/accounts/OAuthGetRequestToken", parameters)

        ' Second: Redirect to Authorization and Fire up the browser
        System.Diagnostics.Process.Start( _
            myOAuth.AuthorizeToken("https://www.google.com/accounts/OAuthAuthorizeToken"))

        Console.Write("Please enter the key: ")
        Dim key As String = Console.ReadLine().Trim()

        ' Third: Fetch Access Token
        myOAuth.AccessToken("https://www.google.com/accounts/OAuthGetAccessToken", key)

        ' Get XOAuth key for IMAP.
        Dim smtpXOAuthKey As String = myOAuth.GetXOAuthKey( _
            String.Format("https://mail.google.com/mail/b/{0}/smtp/", userEmail))

        ' Last: Login to Gmail SMTP server using XOAuth.

        ' Connect to the server, login and send e-mail.
        Dim server As SmtpServer = New SmtpServer("smtp.gmail.com", 465, 0)
        server.AuthMethods = AuthenticationMethods.SaslOAuth
        server.AccountName = Nothing
        server.Password = smtpXOAuthKey

        Dim mailer As Smtp = New Smtp

        mailer.From.AsString = userEmail
        mailer.To.Add("e-mail@host.com")
        mailer.Subject = "My subject"
        mailer.BodyPlainText = "Text body"

        mailer.SmtpServers.Add(server)
        mailer.Send()
    End Sub
End Class

This sample authenticates client in Yahoo OAuth with keys, received after application registration in Yahoo. See Yahoo! OAuth Quick Start Guide for additional information.
[C#]
using System;
using System.Collections.Specialized;
using MailBee;

class Sample
{
    static void Main(string[] args)
    {
        string consumerKey = "";
        string consumerSecret = "";

        Console.Write("Please enter your email: ");
        string userEmail = Console.ReadLine().Trim();

        OAuth myOAuth = new OAuth(consumerKey, consumerSecret);

        StringDictionary parameters = new StringDictionary();
        parameters.Add(OAuth.OAuthCallbackKey, "oob");

        // First: Fetch Request Token
        myOAuth.RequestToken("https://api.login.yahoo.com/oauth/v2/get_request_token", parameters);

        // Second: Redirect to Authorization and Fire up the browser
        System.Diagnostics.Process.Start(
            myOAuth.AuthorizeToken("https://api.login.yahoo.com/oauth/v2/request_auth"));

        Console.Write("Please enter the key: ");
        string key = Console.ReadLine().Trim();

        // Third: Fetch Access Token
        myOAuth.AccessToken("https://api.login.yahoo.com/oauth/v2/get_token", key);

        Console.WriteLine("Token: " + myOAuth.Token);
        Console.WriteLine("Token Secret: " + myOAuth.TokenSecret);
        Console.ReadLine();
    }
}
[Visual Basic]
Imports System.Collections.Specialized
Imports MailBee

Class Sample
    Shared Sub Main(ByVal args() As String)
        Dim consumerKey As String = ""
        Dim consumerSecret As String = ""

        Console.Write("Please enter your email: ")
        Dim userEmail As String = Console.ReadLine().Trim()

        Dim myOAuth As OAuth = New OAuth(consumerKey, consumerSecret)

        Dim parameters As StringDictionary = New StringDictionary
        parameters.Add(OAuth.OAuthCallbackKey, "oob")

        ' First: Fetch Request Token
        myOAuth.RequestToken("https://api.login.yahoo.com/oauth/v2/get_request_token", parameters)

        ' Second: Redirect to Authorization and Fire up the browser
        System.Diagnostics.Process.Start( _
         myOAuth.AuthorizeToken("https://api.login.yahoo.com/oauth/v2/request_auth"))

        Console.Write("Please enter the key: ")
        Dim key As String = Console.ReadLine().Trim()

        ' Third: Fetch Access Token
        myOAuth.AccessToken("https://api.login.yahoo.com/oauth/v2/get_token", key)

        Console.WriteLine("Token: " + myOAuth.Token)
        Console.WriteLine("Token Secret: " + myOAuth.TokenSecret)
        Console.ReadLine()
    End Sub
End Class

Requirements

Namespace: MailBee

Assembly: MailBee.NET (in MailBee.NET.dll)

See Also

OAuth Members | MailBee Namespace