OAuth Class
This class provides methods for OAuth 1.0 authentication process.
Inheritance Hierarchy
SystemObject
  MailBeeOAuth

Namespace: MailBee
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public class OAuth

The OAuth type exposes the following members.

Constructors
  NameDescription
Public methodOAuth
Initializes a new instance of OAuth object with a consumer key pair.
Top
Methods
  NameDescription
Public methodCode exampleAccessToken(String, String)
Fetches Request Access from the specified URI
Public methodCode exampleAccessToken(String, String, StringDictionary)
Fetches Request Access from the specified URI passing extra parameters in that URI.
Public methodCode exampleAuthorizeToken(String)
Fetches Request Authorize from the specified URI (URL).
Public methodCode exampleAuthorizeToken(String, StringDictionary)
Fetches Request Authorize from the specified URI passing extra parameters in that URI.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodCode exampleGetXOAuthKey(String)
Fetches XOAuth key from the specified URI.
Public methodCode exampleGetXOAuthKey(String, StringDictionary)
Fetches XOAuth key from the specified URI passing extra parameters in that URI.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleRequestToken(String)
Fetches Request Token from the specified URI.
Public methodCode exampleRequestToken(String, StringDictionary)
Fetches Request Token from the specified URI passing extra parameters in that URI.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Fields
  NameDescription
Public fieldStatic memberOAuthCallbackKey
The parameter name for setting callback URI.
Top
Properties
  NameDescription
Public propertyEnableOpenIDHybrid
Enables or disables OpenID + OAuth mode.
Public propertyToken
Gets token key from OAuth provider.
Public propertyTokenSecret
Gets token secret key from OAuth provider.
Top
Remarks
OAuth 1.0 is now considered deprecated. Use OAuth2 whenever possible. OAuth 2.0 in Windows and ASP.NET MVC apps guide provides lots of samples and detailed explanations on various aspects of OAuth 2.0 usage with Microsoft and Google providers.
Examples

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

The first sample authenticates client in Gmail IMAP via 2-legged OAuth.

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();
    }
}
Examples

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.
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();
    }
}
Examples

This sample authenticates client in Gmail SMTP via 3-legged OAuth with "anonymous" consumer key pairs and sends e-mail message.
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();
    }
}
Examples

This sample authenticates client in Yahoo OAuth with keys, received after application registration in Yahoo. See Yahoo! OAuth Quick Start Guide for additional information.
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();
    }
}
See Also